|
11 | 11 | import com.launchdarkly.sdk.server.DataModel.FeatureFlag; |
12 | 12 | import com.launchdarkly.sdk.server.DataModel.Operator; |
13 | 13 | import com.launchdarkly.sdk.server.DataModel.Prerequisite; |
| 14 | +import com.launchdarkly.sdk.server.DataModel.Rollout; |
14 | 15 | import com.launchdarkly.sdk.server.DataModel.RolloutKind; |
15 | 16 | import com.launchdarkly.sdk.server.DataModel.Rule; |
16 | 17 | import com.launchdarkly.sdk.server.DataModel.Segment; |
@@ -575,6 +576,89 @@ public void explicitNullsAreToleratedForNullableValues() { |
575 | 576 | ); |
576 | 577 | } |
577 | 578 |
|
| 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 | + |
578 | 662 | @Test |
579 | 663 | public void parsingFullDataSetEmptyObject() throws Exception { |
580 | 664 | String json = "{}"; |
|
0 commit comments