From 14238ee870ffd0636ae4d0352fc94a5e017ce83d Mon Sep 17 00:00:00 2001 From: Adam Roberts Date: Thu, 21 Mar 2024 13:11:21 -0400 Subject: [PATCH] fix: issue #859 Fixed the logic to allow the tests to pass by copying the passed in attributes --- src/main/java/dev/openfeature/sdk/AbstractStructure.java | 2 +- src/main/java/dev/openfeature/sdk/ImmutableContext.java | 7 +++++-- src/main/java/dev/openfeature/sdk/MutableContext.java | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) 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