diff --git a/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java b/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java index e067903..ef356a1 100644 --- a/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java +++ b/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java @@ -143,8 +143,9 @@ private LDContext BuildSingleContext(Map attributes, String kind, ContextBuilder builder = LDContext.builder(ContextKind.of(kind), key); attributes.forEach((attrKey, attrValue) -> { - // Key has been processed, so we can skip it. - if (Objects.equals(attrKey, "key") || Objects.equals(attrKey, "targetingKey")) return; + // Key and kind have been processed, so we can skip them. + if (Objects.equals(attrKey, "key") || Objects.equals(attrKey, "targetingKey") + || Objects.equals(attrKey, "kind")) return; if (Objects.equals(attrKey, "privateAttributes")) { List valueList = attrValue.asList(); diff --git a/src/test/java/com/launchdarkly/openfeature/serverprovider/ContextConverterTest.java b/src/test/java/com/launchdarkly/openfeature/serverprovider/ContextConverterTest.java index cebb687..370b400 100644 --- a/src/test/java/com/launchdarkly/openfeature/serverprovider/ContextConverterTest.java +++ b/src/test/java/com/launchdarkly/openfeature/serverprovider/ContextConverterTest.java @@ -53,6 +53,38 @@ public void itCanCreateAContextFromAKeyAndKind() { assertFalse(logs().containsAnyLogs()); } + @Test + public void itIgnoresNestedKindInMultiKindContexts() { + for (Value nestedKind : new Value[]{new Value("user"), new Value("multi"), new Value(42)}) { + HashMap orgAttributes = new HashMap<>(); + orgAttributes.put("key", new Value("org-key")); + orgAttributes.put("kind", nestedKind); + HashMap userAttributes = new HashMap<>(); + userAttributes.put("key", new Value("user-key")); + HashMap attributes = new HashMap<>(); + attributes.put("kind", new Value("multi")); + attributes.put("org", new Value(new ImmutableStructure(orgAttributes))); + attributes.put("user", new Value(new ImmutableStructure(userAttributes))); + + LDContext converted = evaluationContextConverter.toLdContext(new ImmutableContext(attributes)); + + assertTrue(converted.isValid()); + assertEquals(2, converted.getIndividualContextCount()); + LDContext org = converted.getIndividualContext(ContextKind.of("org")); + assertNotNull(org); + assertEquals(ContextKind.of("org"), org.getKind()); + assertEquals("org-key", org.getKey()); + for (String attribute : org.getCustomAttributeNames()) { + assertNotEquals("kind", attribute); + } + assertEquals(LDContext.create("user-key"), converted.getIndividualContext(ContextKind.of("user"))); + assertEquals(LDContext.createMulti( + LDContext.builder(ContextKind.of("org"), "org-key").build(), + LDContext.create("user-key")), converted); + assertFalse(logs().containsAnyLogs()); + } + } + @Test public void itLogsAnErrorWhenThereIsNoTargetingKey() { evaluationContextConverter.toLdContext(new ImmutableContext());