Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 3063cd2

Browse files
committed
treat unsupported operators as a non-match, don't throw NPE
1 parent 1612b1f commit 3063cd2

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/main/java/com/launchdarkly/client/Clause.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ boolean matchesUser(FeatureStore store, LDUser user) {
7575
}
7676

7777
private boolean matchAny(JsonPrimitive userValue) {
78-
for (JsonPrimitive v : values) {
79-
if (op.apply(userValue, v)) {
80-
return true;
78+
if (op != null) {
79+
for (JsonPrimitive v : values) {
80+
if (op.apply(userValue, v)) {
81+
return true;
82+
}
8183
}
8284
}
8385
return false;

src/test/java/com/launchdarkly/client/FeatureFlagTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,34 @@ public void clauseCanBeNegated() throws Exception {
243243
assertEquals(jbool(false), f.evaluate(user, featureStore).getValue());
244244
}
245245

246+
@Test
247+
public void clauseWithNullOperatorDoesNotMatch() throws Exception {
248+
// Operator will be null if it failed to be unmarshaled from JSON, i.e. it's not a supported enum value
249+
Clause badClause = new Clause("name", null, Arrays.asList(js("Bob")), false);
250+
FeatureFlag f = booleanFlagWithClauses(badClause);
251+
LDUser user = new LDUser.Builder("key").name("Bob").build();
252+
253+
assertEquals(jbool(false), f.evaluate(user, featureStore).getValue());
254+
}
255+
256+
@Test
257+
public void clauseWithNullOperatorDoesNotStopSubsequentRuleFromMatching() throws Exception {
258+
Clause badClause = new Clause("name", null, Arrays.asList(js("Bob")), false);
259+
Rule badRule = new Rule(Arrays.asList(badClause), 1, null);
260+
Clause goodClause = new Clause("name", Operator.in, Arrays.asList(js("Bob")), false);
261+
Rule goodRule = new Rule(Arrays.asList(goodClause), 1, null);
262+
FeatureFlag f = new FeatureFlagBuilder("feature")
263+
.on(true)
264+
.rules(Arrays.asList(badRule, goodRule))
265+
.fallthrough(fallthroughVariation(0))
266+
.offVariation(0)
267+
.variations(jbool(false), jbool(true))
268+
.build();
269+
LDUser user = new LDUser.Builder("key").name("Bob").build();
270+
271+
assertEquals(jbool(true), f.evaluate(user, featureStore).getValue());
272+
}
273+
246274
@Test
247275
public void testSegmentMatchClauseRetrievesSegmentFromStore() throws Exception {
248276
Segment segment = new Segment.Builder("segkey")

0 commit comments

Comments
 (0)