From f1720704bbb8a6a04e031bd9ffbfbc3af52b5745 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 12 Jul 2024 09:46:01 -0400 Subject: [PATCH 1/2] fix: Log warning if targeting key is not a string --- ld_openfeature/impl/context_converter.py | 2 +- tests/impl/test_context_converter.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ld_openfeature/impl/context_converter.py b/ld_openfeature/impl/context_converter.py index 5564c79..7424ee9 100644 --- a/ld_openfeature/impl/context_converter.py +++ b/ld_openfeature/impl/context_converter.py @@ -49,7 +49,7 @@ def __get_targeting_key(self, targeting_key: Optional[str], key: Any) -> str: if key is not None and isinstance(key, str): targeting_key = targeting_key if targeting_key else key - if targeting_key is None or targeting_key == "": + if targeting_key is None or targeting_key == "" or not isinstance(targeting_key, str): logger.error("The EvaluationContext must contain either a 'targetingKey' or a 'key' and the type must be a string.") return targeting_key if targeting_key else "" diff --git a/tests/impl/test_context_converter.py b/tests/impl/test_context_converter.py index 36a82b5..d8ef790 100644 --- a/tests/impl/test_context_converter.py +++ b/tests/impl/test_context_converter.py @@ -28,6 +28,16 @@ def test_create_context_with_invalid_key(context_converter: EvaluationContextCon assert caplog.records[0].message == "A non-string 'key' attribute was provided." +def test_create_context_with_invalid_targeting_key(context_converter: EvaluationContextConverter, caplog): + context = EvaluationContext(False) + ld_context = context_converter.to_ld_context(context) + + assert ld_context.valid is False + assert ld_context.key == '' + + assert caplog.records[0].message == "The EvaluationContext must contain either a 'targetingKey' or a 'key' and the type must be a string." + + def test_invalid_private_attribute_types_are_ignored(context_converter: EvaluationContextConverter, caplog): context = EvaluationContext("user-key", {"privateAttributes": [True]}) ld_context = context_converter.to_ld_context(context) From b262e2479ba9c280271f61e576a84c253261905c Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 12 Jul 2024 10:31:45 -0400 Subject: [PATCH 2/2] ignore lint warning in tests --- tests/impl/test_context_converter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/impl/test_context_converter.py b/tests/impl/test_context_converter.py index d8ef790..70b9ef4 100644 --- a/tests/impl/test_context_converter.py +++ b/tests/impl/test_context_converter.py @@ -29,7 +29,7 @@ def test_create_context_with_invalid_key(context_converter: EvaluationContextCon def test_create_context_with_invalid_targeting_key(context_converter: EvaluationContextConverter, caplog): - context = EvaluationContext(False) + context = EvaluationContext(False) # type: ignore[arg-type] ld_context = context_converter.to_ld_context(context) assert ld_context.valid is False