Skip to content

Commit ab9078c

Browse files
ran-isenbergRan Isenbergheitorlessa
authored
fix(feature_flags): handle expected falsy values in conditions (#2052)
Co-authored-by: Ran Isenberg <[email protected]> Co-authored-by: Heitor Lessa <[email protected]> Co-authored-by: Ajwad Shaikh @ajwad-shaikh
1 parent de69eca commit ab9078c

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

Diff for: aws_lambda_powertools/utilities/feature_flags/feature_flags.py

-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ def __init__(self, store: StoreProvider, logger: Optional[Union[logging.Logger,
4747
self.logger = logger or logging.getLogger(__name__)
4848

4949
def _match_by_action(self, action: str, condition_value: Any, context_value: Any) -> bool:
50-
if not context_value:
51-
return False
5250
mapping_by_action = {
5351
schema.RuleAction.EQUALS.value: lambda a, b: a == b,
5452
schema.RuleAction.NOT_EQUALS.value: lambda a, b: a != b,

Diff for: aws_lambda_powertools/utilities/feature_flags/schema.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,9 @@ def validate_condition_key(condition: Dict[str, Any], rule_name: str):
325325

326326
@staticmethod
327327
def validate_condition_value(condition: Dict[str, Any], rule_name: str):
328-
value = condition.get(CONDITION_VALUE, "")
329-
if not value:
330-
raise SchemaValidationError(f"'value' key must not be empty, rule={rule_name}")
328+
value = condition.get(CONDITION_VALUE)
329+
if value is None:
330+
raise SchemaValidationError(f"'value' key must not be null, rule={rule_name}")
331331
action = condition.get(CONDITION_ACTION, "")
332332

333333
# time actions need to be parsed to make sure date and time format is valid and timezone is recognized

Diff for: tests/functional/feature_flags/test_schema_validation.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,47 @@ def test_validate_condition_missing_condition_value():
301301
}
302302

303303
# WHEN calling validate_condition
304-
with pytest.raises(SchemaValidationError, match="'value' key must not be empty"):
304+
with pytest.raises(SchemaValidationError, match="'value' key must not be null"):
305305
ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")
306306

307307

308+
def test_validate_condition_none_condition_value():
309+
# GIVEN a configuration with a missing condition value
310+
condition = {
311+
"action": RuleAction.EQUALS.value,
312+
"key": "tenant_id",
313+
"value": None,
314+
}
315+
316+
# WHEN calling validate_condition
317+
with pytest.raises(SchemaValidationError, match="'value' key must not be null"):
318+
ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")
319+
320+
321+
def test_validate_condition_empty_condition_value():
322+
# GIVEN a configuration with a missing condition value
323+
condition = {
324+
"action": RuleAction.EQUALS.value,
325+
"key": "tenant_id",
326+
"value": "",
327+
}
328+
329+
# WHEN calling validate_condition
330+
ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")
331+
332+
333+
def test_validate_condition_valid_falsy_condition_value():
334+
# GIVEN a configuration with a missing condition value
335+
condition = {
336+
"action": RuleAction.EQUALS.value,
337+
"key": "tenant_id",
338+
"value": 0,
339+
}
340+
341+
# WHEN calling validate_condition
342+
ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")
343+
344+
308345
def test_validate_rule_invalid_rule_type():
309346
# GIVEN an invalid rule type of empty list
310347
# WHEN calling validate_rule

0 commit comments

Comments
 (0)