diff --git a/src/main/java/dev/openfeature/sdk/AbstractStructure.java b/src/main/java/dev/openfeature/sdk/AbstractStructure.java index a7d7c2eae..e50fbe920 100644 --- a/src/main/java/dev/openfeature/sdk/AbstractStructure.java +++ b/src/main/java/dev/openfeature/sdk/AbstractStructure.java @@ -13,7 +13,7 @@ abstract class AbstractStructure implements Structure { } AbstractStructure(Map attributes) { - this.attributes = attributes; + this.attributes = new HashMap<>(attributes); } /** diff --git a/src/main/java/dev/openfeature/sdk/ImmutableContext.java b/src/main/java/dev/openfeature/sdk/ImmutableContext.java index 632448aae..1a1db8ec5 100644 --- a/src/main/java/dev/openfeature/sdk/ImmutableContext.java +++ b/src/main/java/dev/openfeature/sdk/ImmutableContext.java @@ -52,9 +52,12 @@ public ImmutableContext(Map attributes) { */ public ImmutableContext(String targetingKey, Map attributes) { if (targetingKey != null && !targetingKey.trim().isEmpty()) { - attributes.put(TARGETING_KEY, new Value(targetingKey)); + final Map actualAttribs = new HashMap<>(attributes); + actualAttribs.put(TARGETING_KEY, new Value(targetingKey)); + this.structure = new ImmutableStructure(actualAttribs); + } else { + this.structure = new ImmutableStructure(attributes); } - this.structure = new ImmutableStructure(attributes); } /** diff --git a/src/main/java/dev/openfeature/sdk/MutableContext.java b/src/main/java/dev/openfeature/sdk/MutableContext.java index 69c22b841..bd6c8affb 100644 --- a/src/main/java/dev/openfeature/sdk/MutableContext.java +++ b/src/main/java/dev/openfeature/sdk/MutableContext.java @@ -42,10 +42,10 @@ public MutableContext(Map attributes) { * @param attributes evaluation context attributes */ public MutableContext(String targetingKey, Map attributes) { + this.structure = new MutableStructure(attributes); if (targetingKey != null && !targetingKey.trim().isEmpty()) { - attributes.put(TARGETING_KEY, new Value(targetingKey)); + this.structure.attributes.put(TARGETING_KEY, new Value(targetingKey)); } - this.structure = new MutableStructure(attributes); } // override @Delegate methods so that we can use "add" methods and still return MutableContext, not Structure