Skip to content

Commit 7c83055

Browse files
committed
fixup: clamping impl consistency
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
1 parent c46de58 commit 7c83055

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/resolvers/process/custom_ops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def _parse_fraction(arg: JsonLogicArg) -> Fraction:
9191

9292
fraction = Fraction(variant=variant)
9393
if weight is not None:
94-
fraction.weight = weight
94+
# negative weights can be the result of rollout calculations, so we clamp to 0 rather than raising an error
95+
fraction.weight = max(0, weight)
9596

9697
return fraction
9798

providers/openfeature-provider-flagd/tests/test_targeting.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ def test_weight_zero_bucket_never_wins(self):
237237
logic = targeting("flagA", rule, EvaluationContext(targeting_key="any"))
238238
assert logic == "always"
239239

240+
def test_negative_weight_clamped_to_zero(self):
241+
rule = {
242+
"fractional": [
243+
["on", -1000],
244+
["off", 1],
245+
],
246+
}
247+
logic = targeting("flagA", rule, EvaluationContext(targeting_key="any"))
248+
assert logic == "off"
249+
240250
def test_weight_as_fractional_float_is_invalid(self):
241251
rule = {
242252
"fractional": [

0 commit comments

Comments
 (0)