From 6a96a060e8bb9b69e202862d1b160e3941d4df00 Mon Sep 17 00:00:00 2001 From: termag agent runtime Date: Thu, 19 Nov 2026 19:55:50 +0000 Subject: [PATCH] fix: Keep clientSideAvailability when the SDK writes a flag to a persistent store FeatureFlag did not model clientSideAvailability, so the SDK dropped the object when it wrote a flag to Redis, Consul or DynamoDB. A Go SDK or the Relay Proxy then read usingMobileKey as true for that flag. FeatureFlag now keeps the object if the JSON has it. When the object is present, clientSide takes the value of usingEnvironmentId, as in the Go SDK, and the SDK writes the object with both properties. A flag without the object keeps its current encoding and behavior. Generated-by: termag --- .../launchdarkly/sdk/server/DataModel.java | 45 ++++++++- .../server/DataModelPreprocessingTest.java | 16 ++-- .../server/DataModelSerializationTest.java | 95 +++++++++++++++++++ .../sdk/server/MigrationOpTrackerTests.java | 2 +- .../sdk/server/ModelBuilders.java | 9 +- 5 files changed, 154 insertions(+), 13 deletions(-) diff --git a/lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/DataModel.java b/lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/DataModel.java index 1e919cfee..e00b6e4d7 100644 --- a/lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/DataModel.java +++ b/lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/DataModel.java @@ -137,6 +137,7 @@ static final class FeatureFlag implements VersionedData, JsonHelpers.PostProcess private Integer offVariation; //optional private List variations; private boolean clientSide; + private ClientSideAvailability clientSideAvailability; // optional private boolean trackEvents; private boolean trackEventsFallthrough; private Long debugEventsUntilDate; @@ -145,6 +146,30 @@ static final class FeatureFlag implements VersionedData, JsonHelpers.PostProcess private Migration migration; private boolean excludeFromSummaries; + /** + * Tells which kinds of client-side SDK can use the flag. The flag has this object only if its JSON + * has a "clientSideAvailability" object. + */ + static final class ClientSideAvailability { + private boolean usingMobileKey; + private boolean usingEnvironmentId; + + ClientSideAvailability() {} + + ClientSideAvailability(boolean usingMobileKey, boolean usingEnvironmentId) { + this.usingMobileKey = usingMobileKey; + this.usingEnvironmentId = usingEnvironmentId; + } + + boolean isUsingMobileKey() { + return usingMobileKey; + } + + boolean isUsingEnvironmentId() { + return usingEnvironmentId; + } + } + /** * Container for migration specific flag data. */ @@ -168,8 +193,9 @@ public Long getCheckRatio() { FeatureFlag(String key, int version, boolean on, List prerequisites, String salt, List targets, List contextTargets, List rules, VariationOrRollout fallthrough, Integer offVariation, - List variations, boolean clientSide, boolean trackEvents, boolean trackEventsFallthrough, - Long debugEventsUntilDate, boolean deleted, Long samplingRatio, Migration migration, boolean excludeFromSummaries) { + List variations, boolean clientSide, ClientSideAvailability clientSideAvailability, + boolean trackEvents, boolean trackEventsFallthrough, Long debugEventsUntilDate, boolean deleted, + Long samplingRatio, Migration migration, boolean excludeFromSummaries) { this.key = key; this.version = version; this.on = on; @@ -181,7 +207,9 @@ public Long getCheckRatio() { this.fallthrough = fallthrough; this.offVariation = offVariation; this.variations = variations; - this.clientSide = clientSide; + // If the flag has a "clientSideAvailability" object, "clientSide" has the value of "usingEnvironmentId". + this.clientSide = clientSideAvailability == null ? clientSide : clientSideAvailability.isUsingEnvironmentId(); + this.clientSideAvailability = clientSideAvailability; this.trackEvents = trackEvents; this.trackEventsFallthrough = trackEventsFallthrough; this.debugEventsUntilDate = debugEventsUntilDate; @@ -259,6 +287,11 @@ boolean isClientSide() { return clientSide; } + // Returns null if the JSON for the flag has no "clientSideAvailability" object. + ClientSideAvailability getClientSideAvailability() { + return clientSideAvailability; + } + Long getSamplingRatio() { return samplingRatio; } Migration getMigration() { return migration; } @@ -268,6 +301,12 @@ boolean isExcludeFromSummaries() { } public void afterDeserialized() { + // The "clientSideAvailability" object is in the newer schema. If the flag has the object, set + // "clientSide" to the value of "usingEnvironmentId". The SDK then writes the two properties + // with the same value, for SDKs that read only "clientSide". + if (clientSideAvailability != null) { + clientSide = clientSideAvailability.isUsingEnvironmentId(); + } DataModelPreprocessing.preprocessFlag(this); } } diff --git a/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/DataModelPreprocessingTest.java b/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/DataModelPreprocessingTest.java index 0fb0fc9c0..ce861b49c 100644 --- a/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/DataModelPreprocessingTest.java +++ b/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/DataModelPreprocessingTest.java @@ -41,7 +41,7 @@ public class DataModelPreprocessingTest { private FeatureFlag flagFromClause(Clause c) { return new FeatureFlag("key", 0, false, null, null, null, null, rulesFromClause(c), - null, null, null, false, false, false, null, false, null, null, false); + null, null, null, false, null, false, false, null, false, null, null, false); } private List rulesFromClause(Clause c) { @@ -54,7 +54,7 @@ public void preprocessFlagAddsPrecomputedOffResult() { ImmutableList.of(), null, 0, ImmutableList.of(aValue, bValue), - false, false, false, null, false, null, null, false); + false, null, false, false, null, false, null, null, false); f.afterDeserialized(); @@ -69,7 +69,7 @@ public void preprocessFlagAddsPrecomputedOffResultForNullOffVariation() { ImmutableList.of(), null, null, ImmutableList.of(aValue, bValue), - false, false, false, null, false, null, null, false); + false, null, false, false, null, false, null, null, false); f.afterDeserialized(); @@ -83,7 +83,7 @@ public void preprocessFlagAddsPrecomputedFallthroughResults() { FeatureFlag f = new FeatureFlag("key", 0, false, null, null, null, null, ImmutableList.of(), null, 0, ImmutableList.of(aValue, bValue), - false, false, false, null, false, null, null, false); + false, null, false, false, null, false, null, null, false); f.afterDeserialized(); @@ -109,7 +109,7 @@ public void preprocessFlagAddsPrecomputedTargetMatchResults() { ImmutableList.of(new Target(null, ImmutableSet.of(), 1)), null, ImmutableList.of(), null, 0, ImmutableList.of(aValue, bValue), - false, false, false, null, false, null, null, false); + false, null, false, false, null, false, null, null, false); f.afterDeserialized(); @@ -126,7 +126,7 @@ public void preprocessFlagAddsPrecomputedPrerequisiteFailedResults() { null, null, null, ImmutableList.of(), null, 0, ImmutableList.of(aValue, bValue), - false, false, false, null, false, null, null, false); + false, null, false, false, null, false, null, null, false); f.afterDeserialized(); @@ -150,7 +150,7 @@ public void preprocessFlagAddsPrecomputedResultsToFlagRulesWithRollout() { ImmutableList.of(new Rule("ruleid0", ImmutableList.of(), null, rollout, false)), null, null, ImmutableList.of(aValue, bValue), - false, false, false, null, false, null, null, false); + false, null, false, false, null, false, null, null, false); f.afterDeserialized(); @@ -177,7 +177,7 @@ public void preprocessFlagAddsPrecomputedResultsToFlagRulesWithJustVariation() { ImmutableList.of(new Rule("ruleid0", ImmutableList.of(), 0, null, false)), null, null, ImmutableList.of(aValue, bValue), - false, false, false, null, false, null, null, false); + false, null, false, false, null, false, null, null, false); f.afterDeserialized(); diff --git a/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/DataModelSerializationTest.java b/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/DataModelSerializationTest.java index 12b57b2c7..6dbefaaa7 100644 --- a/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/DataModelSerializationTest.java +++ b/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/DataModelSerializationTest.java @@ -659,6 +659,101 @@ public void explicitNullsForOptionalStringAndBooleanPropertiesAreTreatedAsOmitte }); } + @Test + public void flagWithClientSideAvailabilityIsDeserializedAndSerialized() { + boolean[] values = { false, true }; + for (boolean usingMobileKey: values) { + for (boolean usingEnvironmentId: values) { + LDValue flagJson = baseBuilder("flag-key") + .put("clientSide", usingEnvironmentId) + .put("clientSideAvailability", LDValue.buildObject() + .put("usingMobileKey", usingMobileKey) + .put("usingEnvironmentId", usingEnvironmentId) + .build()) + .build(); + assertFlagFromJson(flagJson, flag -> { + assertNotNull(flag.getClientSideAvailability()); + assertEquals(usingMobileKey, flag.getClientSideAvailability().isUsingMobileKey()); + assertEquals(usingEnvironmentId, flag.getClientSideAvailability().isUsingEnvironmentId()); + assertEquals(usingEnvironmentId, flag.isClientSide()); + + LDValue json1 = LDValue.parse(FEATURES.serialize(new ItemDescriptor(flag.getVersion(), flag))); + assertJsonEquals(flagJson.get("clientSideAvailability").toJsonString(), + json1.get("clientSideAvailability").toJsonString()); + assertEquals(LDValue.of(usingEnvironmentId), json1.get("clientSide")); + }); + } + } + } + + @Test + public void flagWithClientSideAvailabilityTakesClientSideFromUsingEnvironmentId() { + LDValue flagJson = baseBuilder("flag-key") + .put("clientSide", true) + .put("clientSideAvailability", LDValue.buildObject() + .put("usingMobileKey", true) + .put("usingEnvironmentId", false) + .build()) + .build(); + assertFlagFromJson(flagJson, flag -> { + // The object has precedence over the "clientSide" value that the flag JSON has. + assertFalse(flag.isClientSide()); + + LDValue json1 = LDValue.parse(FEATURES.serialize(new ItemDescriptor(flag.getVersion(), flag))); + assertEquals(LDValue.of(false), json1.get("clientSide")); + assertEquals(LDValue.of(false), json1.get("clientSideAvailability").get("usingEnvironmentId")); + }); + } + + @Test + public void flagWithClientSideAvailabilityMissingPropertyReadsItAsFalse() { + LDValue flagJson = baseBuilder("flag-key") + .put("clientSide", true) + .put("clientSideAvailability", LDValue.buildObject() + .put("usingEnvironmentId", true) + .build()) + .build(); + assertFlagFromJson(flagJson, flag -> { + assertNotNull(flag.getClientSideAvailability()); + assertFalse(flag.getClientSideAvailability().isUsingMobileKey()); + assertTrue(flag.getClientSideAvailability().isUsingEnvironmentId()); + + LDValue json1 = LDValue.parse(FEATURES.serialize(new ItemDescriptor(flag.getVersion(), flag))); + assertJsonEquals("{\"usingMobileKey\":false,\"usingEnvironmentId\":true}", + json1.get("clientSideAvailability").toJsonString()); + assertEquals(LDValue.of(true), json1.get("clientSide")); + }); + } + + @Test + public void flagWithoutClientSideAvailabilityIsSerializedWithClientSideOnly() { + boolean[] values = { false, true }; + for (boolean clientSide: values) { + LDValue absentJson = baseBuilder("flag-key").put("clientSide", clientSide).build(); + LDValue nullJson = baseBuilder("flag-key").put("clientSide", clientSide) + .put("clientSideAvailability", LDValue.ofNull()).build(); + for (LDValue flagJson: new LDValue[] { absentJson, nullJson }) { + assertFlagFromJson(flagJson, flag -> { + assertNull(flag.getClientSideAvailability()); + assertEquals(clientSide, flag.isClientSide()); + + String json1 = FEATURES.serialize(new ItemDescriptor(flag.getVersion(), flag)); + assertFalse(json1.contains("clientSideAvailability")); + assertTrue(json1.contains("\"clientSide\":" + clientSide)); + }); + } + } + } + + @Test + public void flagWithClientSideAvailabilityFromBuilderIsSerialized() { + FeatureFlag flag = flagBuilder("flag-key").clientSideAvailability(false, true).build(); + LDValue json = LDValue.parse(serialize(flag)); + assertJsonEquals("{\"usingMobileKey\":false,\"usingEnvironmentId\":true}", + json.get("clientSideAvailability").toJsonString()); + assertEquals(LDValue.of(true), json.get("clientSide")); + } + @Test public void parsingFullDataSetEmptyObject() throws Exception { String json = "{}"; diff --git a/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/MigrationOpTrackerTests.java b/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/MigrationOpTrackerTests.java index ca1047d2c..d5d1e8b3e 100644 --- a/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/MigrationOpTrackerTests.java +++ b/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/MigrationOpTrackerTests.java @@ -137,7 +137,7 @@ private MigrationOpTracker trackerWithFlag(EvaluationDetail detail, LDCo "test-key", new DataModel.FeatureFlag("flag", 2, true, null, "salt", null, null, null, null, 0, null, - false, false, false, null, false, + false, null, false, false, null, false, 5l, null, false), detail, MigrationStage.OFF, diff --git a/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/ModelBuilders.java b/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/ModelBuilders.java index b9a6f1ae0..0ee90343c 100644 --- a/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/ModelBuilders.java +++ b/lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/ModelBuilders.java @@ -145,6 +145,7 @@ public static class FlagBuilder { private Integer offVariation; private List variations = new ArrayList<>(); private boolean clientSide; + private FeatureFlag.ClientSideAvailability clientSideAvailability; private boolean trackEvents; private boolean trackEventsFallthrough; private Long debugEventsUntilDate; @@ -173,6 +174,7 @@ private FlagBuilder(DataModel.FeatureFlag f) { this.offVariation = f.getOffVariation(); this.variations = f.getVariations(); this.clientSide = f.isClientSide(); + this.clientSideAvailability = f.getClientSideAvailability(); this.trackEvents = f.isTrackEvents(); this.trackEventsFallthrough = f.isTrackEventsFallthrough(); this.debugEventsUntilDate = f.getDebugEventsUntilDate(); @@ -297,6 +299,11 @@ public FlagBuilder clientSide(boolean clientSide) { return this; } + public FlagBuilder clientSideAvailability(boolean usingMobileKey, boolean usingEnvironmentId) { + this.clientSideAvailability = new FeatureFlag.ClientSideAvailability(usingMobileKey, usingEnvironmentId); + return this; + } + public FlagBuilder trackEvents(boolean trackEvents) { this.trackEvents = trackEvents; return this; @@ -335,7 +342,7 @@ public FlagBuilder migration(FeatureFlag.Migration migration) { public FeatureFlag build() { FeatureFlag flag = new DataModel.FeatureFlag(key, version, on, prerequisites, salt, targets, contextTargets, rules, fallthrough, offVariation, variations, - clientSide, trackEvents, trackEventsFallthrough, debugEventsUntilDate, deleted, + clientSide, clientSideAvailability, trackEvents, trackEventsFallthrough, debugEventsUntilDate, deleted, samplingRatio, migration, excludeFromSummaries); if (!disablePreprocessing) { flag.afterDeserialized();