Skip to content

Commit 7b9e54a

Browse files
committed
fix: Treat a null optional property in flag data as omitted
An explicit null for an optional string or boolean property of a clause, rollout or segment rule made the parser throw, so the whole flag data set was rejected. These properties are now read as when they are omitted. Generated-by: termag
1 parent 9ba9a08 commit 7b9e54a

2 files changed

Lines changed: 114 additions & 7 deletions

File tree

‎lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/DataModelSerialization.java‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public Clause read(JsonReader in) throws IOException {
184184
while (in.hasNext()) {
185185
switch (in.nextName()) {
186186
case "contextKind":
187-
contextKind = ContextKind.of(in.nextString());
187+
contextKind = readNullableContextKind(in);
188188
break;
189189
case "attribute":
190190
attrString = in.nextString();
@@ -205,7 +205,7 @@ public Clause read(JsonReader in) throws IOException {
205205
}
206206
break;
207207
case "negate":
208-
negate = in.nextBoolean();
208+
negate = readBooleanOrFalse(in);
209209
break;
210210
default:
211211
in.skipValue();
@@ -253,7 +253,7 @@ public Rollout read(JsonReader in) throws IOException {
253253
while (in.hasNext()) {
254254
switch (in.nextName()) {
255255
case "contextKind":
256-
contextKind = ContextKind.of(in.nextString());
256+
contextKind = readNullableContextKind(in);
257257
break;
258258
case "variations":
259259
if (in.peek() == JsonToken.NULL) {
@@ -268,10 +268,10 @@ public Rollout read(JsonReader in) throws IOException {
268268
}
269269
break;
270270
case "bucketBy":
271-
bucketByString = in.nextString();
271+
bucketByString = readNullableString(in);
272272
break;
273273
case "kind":
274-
kind = RolloutKind.experiment.name().equals(in.nextString()) ? RolloutKind.experiment :
274+
kind = RolloutKind.experiment.name().equals(readNullableString(in)) ? RolloutKind.experiment :
275275
RolloutKind.rollout;
276276
break;
277277
case "seed":
@@ -334,10 +334,10 @@ public SegmentRule read(JsonReader in) throws IOException {
334334
weight = readNullableInt(in);
335335
break;
336336
case "rolloutContextKind":
337-
rolloutContextKind = ContextKind.of(in.nextString());
337+
rolloutContextKind = readNullableContextKind(in);
338338
break;
339339
case "bucketBy":
340-
bucketByString = in.nextString();
340+
bucketByString = readNullableString(in);
341341
break;
342342
default:
343343
in.skipValue();
@@ -356,6 +356,29 @@ static Integer readNullableInt(JsonReader in) throws IOException {
356356
}
357357
return in.nextInt();
358358
}
359+
360+
static String readNullableString(JsonReader in) throws IOException {
361+
if (in.peek() == JsonToken.NULL) {
362+
in.skipValue();
363+
return null;
364+
}
365+
return in.nextString();
366+
}
367+
368+
// A null optional boolean has the same value as an omitted one, which is false.
369+
static boolean readBooleanOrFalse(JsonReader in) throws IOException {
370+
if (in.peek() == JsonToken.NULL) {
371+
in.skipValue();
372+
return false;
373+
}
374+
return in.nextBoolean();
375+
}
376+
377+
// A null context kind stays null, as when it is omitted. The attribute is then a literal name.
378+
static ContextKind readNullableContextKind(JsonReader in) throws IOException {
379+
String kind = readNullableString(in);
380+
return kind == null ? null : ContextKind.of(kind);
381+
}
359382

360383
static AttributeRef attributeNameOrPath(String attrString, ContextKind contextKind) {
361384
if (attrString == null) {

‎lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/DataModelSerializationTest.java‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.launchdarkly.sdk.server.DataModel.FeatureFlag;
1212
import com.launchdarkly.sdk.server.DataModel.Operator;
1313
import com.launchdarkly.sdk.server.DataModel.Prerequisite;
14+
import com.launchdarkly.sdk.server.DataModel.Rollout;
1415
import com.launchdarkly.sdk.server.DataModel.RolloutKind;
1516
import com.launchdarkly.sdk.server.DataModel.Rule;
1617
import com.launchdarkly.sdk.server.DataModel.Segment;
@@ -575,6 +576,89 @@ public void explicitNullsAreToleratedForNullableValues() {
575576
);
576577
}
577578

579+
@Test
580+
public void explicitNullsForOptionalStringAndBooleanPropertiesAreTreatedAsOmitted() {
581+
// Parse a flag with a null value for each optional string and boolean property.
582+
LDValue flagJson = baseBuilder("flag-key")
583+
.put("salt", LDValue.ofNull())
584+
.put("trackEvents", LDValue.ofNull())
585+
.put("trackEventsFallthrough", LDValue.ofNull())
586+
.put("clientSide", LDValue.ofNull())
587+
.put("deleted", LDValue.ofNull())
588+
.put("samplingRatio", LDValue.ofNull())
589+
.put("excludeFromSummaries", LDValue.ofNull())
590+
.put("contextTargets", LDValue.arrayOf(
591+
LDValue.buildObject().put("contextKind", LDValue.ofNull()).put("variation", 0)
592+
.put("values", LDValue.arrayOf(LDValue.of("key1"))).build()
593+
))
594+
.put("rules", LDValue.arrayOf(
595+
LDValue.buildObject().put("id", LDValue.ofNull()).put("trackEvents", LDValue.ofNull())
596+
.put("clauses", LDValue.arrayOf(
597+
LDValue.buildObject().put("contextKind", LDValue.ofNull()).put("attribute", "/attr1")
598+
.put("op", "in").put("values", LDValue.arrayOf(LDValue.of("a")))
599+
.put("negate", LDValue.ofNull()).build()
600+
))
601+
.put("rollout", LDValue.buildObject().put("kind", LDValue.ofNull())
602+
.put("contextKind", LDValue.ofNull()).put("bucketBy", LDValue.ofNull())
603+
.put("variations", LDValue.arrayOf(
604+
LDValue.buildObject().put("variation", 0).put("weight", 100000)
605+
.put("untracked", LDValue.ofNull()).build()
606+
)).build())
607+
.build()
608+
))
609+
.build();
610+
// Each null property has the value it has when the property is omitted.
611+
assertFlagFromJson(flagJson, flag -> {
612+
assertNull(flag.getSalt());
613+
assertFalse(flag.isTrackEvents());
614+
assertFalse(flag.isTrackEventsFallthrough());
615+
assertFalse(flag.isClientSide());
616+
assertFalse(flag.isDeleted());
617+
assertNull(flag.getSamplingRatio());
618+
assertFalse(flag.isExcludeFromSummaries());
619+
assertNull(flag.getContextTargets().get(0).getContextKind());
620+
621+
Rule rule = flag.getRules().get(0);
622+
assertNull(rule.getId());
623+
assertFalse(rule.isTrackEvents());
624+
625+
Clause clause = rule.getClauses().get(0);
626+
assertNull(clause.getContextKind());
627+
assertEquals(AttributeRef.fromLiteral("/attr1"), clause.getAttribute());
628+
assertFalse(clause.isNegate());
629+
630+
Rollout rollout = rule.getRollout();
631+
assertEquals(RolloutKind.rollout, rollout.getKind());
632+
assertNull(rollout.getContextKind());
633+
assertNull(rollout.getBucketBy());
634+
assertFalse(rollout.getVariations().get(0).isUntracked());
635+
});
636+
637+
// Parse a segment with a null value for each optional string and boolean property.
638+
LDValue segmentJson = baseBuilder("segment-key")
639+
.put("salt", LDValue.ofNull())
640+
.put("unbounded", LDValue.ofNull())
641+
.put("unboundedContextKind", LDValue.ofNull())
642+
.put("deleted", LDValue.ofNull())
643+
.put("rules", LDValue.arrayOf(
644+
LDValue.buildObject().put("id", LDValue.ofNull())
645+
.put("rolloutContextKind", LDValue.ofNull()).put("bucketBy", LDValue.ofNull())
646+
.put("clauses", LDValue.arrayOf()).build()
647+
))
648+
.build();
649+
// Each null property has the value it has when the property is omitted.
650+
assertSegmentFromJson(segmentJson, segment -> {
651+
assertNull(segment.getSalt());
652+
assertFalse(segment.isUnbounded());
653+
assertNull(segment.getUnboundedContextKind());
654+
assertFalse(segment.isDeleted());
655+
656+
SegmentRule rule = segment.getRules().get(0);
657+
assertNull(rule.getRolloutContextKind());
658+
assertNull(rule.getBucketBy());
659+
});
660+
}
661+
578662
@Test
579663
public void parsingFullDataSetEmptyObject() throws Exception {
580664
String json = "{}";

0 commit comments

Comments
 (0)