Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public Clause read(JsonReader in) throws IOException {
while (in.hasNext()) {
switch (in.nextName()) {
case "contextKind":
contextKind = ContextKind.of(in.nextString());
contextKind = readNullableContextKind(in);
break;
case "attribute":
attrString = in.nextString();
Expand All @@ -205,7 +205,7 @@ public Clause read(JsonReader in) throws IOException {
}
break;
case "negate":
negate = in.nextBoolean();
negate = readBooleanOrFalse(in);
break;
default:
in.skipValue();
Expand Down Expand Up @@ -253,7 +253,7 @@ public Rollout read(JsonReader in) throws IOException {
while (in.hasNext()) {
switch (in.nextName()) {
case "contextKind":
contextKind = ContextKind.of(in.nextString());
contextKind = readNullableContextKind(in);
break;
case "variations":
if (in.peek() == JsonToken.NULL) {
Expand All @@ -268,10 +268,10 @@ public Rollout read(JsonReader in) throws IOException {
}
break;
case "bucketBy":
bucketByString = in.nextString();
bucketByString = readNullableString(in);
break;
case "kind":
kind = RolloutKind.experiment.name().equals(in.nextString()) ? RolloutKind.experiment :
kind = RolloutKind.experiment.name().equals(readNullableString(in)) ? RolloutKind.experiment :
RolloutKind.rollout;
break;
case "seed":
Expand Down Expand Up @@ -334,10 +334,10 @@ public SegmentRule read(JsonReader in) throws IOException {
weight = readNullableInt(in);
break;
case "rolloutContextKind":
rolloutContextKind = ContextKind.of(in.nextString());
rolloutContextKind = readNullableContextKind(in);
break;
case "bucketBy":
bucketByString = in.nextString();
bucketByString = readNullableString(in);
break;
default:
in.skipValue();
Expand All @@ -356,6 +356,29 @@ static Integer readNullableInt(JsonReader in) throws IOException {
}
return in.nextInt();
}

static String readNullableString(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.skipValue();
return null;
}
return in.nextString();
}

// A null optional boolean has the same value as an omitted one, which is false.
static boolean readBooleanOrFalse(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.skipValue();
return false;
}
return in.nextBoolean();
}

// A null context kind stays null, as when it is omitted. The attribute is then a literal name.
static ContextKind readNullableContextKind(JsonReader in) throws IOException {
String kind = readNullableString(in);
return kind == null ? null : ContextKind.of(kind);
}

static AttributeRef attributeNameOrPath(String attrString, ContextKind contextKind) {
if (attrString == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.launchdarkly.sdk.server.DataModel.FeatureFlag;
import com.launchdarkly.sdk.server.DataModel.Operator;
import com.launchdarkly.sdk.server.DataModel.Prerequisite;
import com.launchdarkly.sdk.server.DataModel.Rollout;
import com.launchdarkly.sdk.server.DataModel.RolloutKind;
import com.launchdarkly.sdk.server.DataModel.Rule;
import com.launchdarkly.sdk.server.DataModel.Segment;
Expand Down Expand Up @@ -575,6 +576,89 @@ public void explicitNullsAreToleratedForNullableValues() {
);
}

@Test
public void explicitNullsForOptionalStringAndBooleanPropertiesAreTreatedAsOmitted() {
// Parse a flag with a null value for each optional string and boolean property.
LDValue flagJson = baseBuilder("flag-key")
.put("salt", LDValue.ofNull())
.put("trackEvents", LDValue.ofNull())
.put("trackEventsFallthrough", LDValue.ofNull())
.put("clientSide", LDValue.ofNull())
.put("deleted", LDValue.ofNull())
.put("samplingRatio", LDValue.ofNull())
.put("excludeFromSummaries", LDValue.ofNull())
.put("contextTargets", LDValue.arrayOf(
LDValue.buildObject().put("contextKind", LDValue.ofNull()).put("variation", 0)
.put("values", LDValue.arrayOf(LDValue.of("key1"))).build()
))
.put("rules", LDValue.arrayOf(
LDValue.buildObject().put("id", LDValue.ofNull()).put("trackEvents", LDValue.ofNull())
.put("clauses", LDValue.arrayOf(
LDValue.buildObject().put("contextKind", LDValue.ofNull()).put("attribute", "/attr1")
.put("op", "in").put("values", LDValue.arrayOf(LDValue.of("a")))
.put("negate", LDValue.ofNull()).build()
))
.put("rollout", LDValue.buildObject().put("kind", LDValue.ofNull())
.put("contextKind", LDValue.ofNull()).put("bucketBy", LDValue.ofNull())
.put("variations", LDValue.arrayOf(
LDValue.buildObject().put("variation", 0).put("weight", 100000)
.put("untracked", LDValue.ofNull()).build()
)).build())
.build()
))
.build();
// Each null property has the value it has when the property is omitted.
assertFlagFromJson(flagJson, flag -> {
assertNull(flag.getSalt());
assertFalse(flag.isTrackEvents());
assertFalse(flag.isTrackEventsFallthrough());
assertFalse(flag.isClientSide());
assertFalse(flag.isDeleted());
assertNull(flag.getSamplingRatio());
assertFalse(flag.isExcludeFromSummaries());
assertNull(flag.getContextTargets().get(0).getContextKind());

Rule rule = flag.getRules().get(0);
assertNull(rule.getId());
assertFalse(rule.isTrackEvents());

Clause clause = rule.getClauses().get(0);
assertNull(clause.getContextKind());
assertEquals(AttributeRef.fromLiteral("/attr1"), clause.getAttribute());
assertFalse(clause.isNegate());

Rollout rollout = rule.getRollout();
assertEquals(RolloutKind.rollout, rollout.getKind());
assertNull(rollout.getContextKind());
assertNull(rollout.getBucketBy());
assertFalse(rollout.getVariations().get(0).isUntracked());
});

// Parse a segment with a null value for each optional string and boolean property.
LDValue segmentJson = baseBuilder("segment-key")
.put("salt", LDValue.ofNull())
.put("unbounded", LDValue.ofNull())
.put("unboundedContextKind", LDValue.ofNull())
.put("deleted", LDValue.ofNull())
.put("rules", LDValue.arrayOf(
LDValue.buildObject().put("id", LDValue.ofNull())
.put("rolloutContextKind", LDValue.ofNull()).put("bucketBy", LDValue.ofNull())
.put("clauses", LDValue.arrayOf()).build()
))
.build();
// Each null property has the value it has when the property is omitted.
assertSegmentFromJson(segmentJson, segment -> {
assertNull(segment.getSalt());
assertFalse(segment.isUnbounded());
assertNull(segment.getUnboundedContextKind());
assertFalse(segment.isDeleted());

SegmentRule rule = segment.getRules().get(0);
assertNull(rule.getRolloutContextKind());
assertNull(rule.getBucketBy());
});
}

@Test
public void parsingFullDataSetEmptyObject() throws Exception {
String json = "{}";
Expand Down
Loading