Skip to content

Commit d51cacb

Browse files
fix: issue #859 (#860)
* fix: issue #859 Added failing tests Signed-off-by: Adam Roberts <[email protected]> * fix: issue #859 Fixed the logic to allow the tests to pass by copying the passed in attributes Signed-off-by: Adam Roberts <[email protected]> --------- Signed-off-by: Adam Roberts <[email protected]>
1 parent 675de14 commit d51cacb

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

src/main/java/dev/openfeature/sdk/AbstractStructure.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class AbstractStructure implements Structure {
1313
}
1414

1515
AbstractStructure(Map<String, Value> attributes) {
16-
this.attributes = attributes;
16+
this.attributes = new HashMap<>(attributes);
1717
}
1818

1919
/**

src/main/java/dev/openfeature/sdk/ImmutableContext.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ public ImmutableContext(Map<String, Value> attributes) {
5252
*/
5353
public ImmutableContext(String targetingKey, Map<String, Value> attributes) {
5454
if (targetingKey != null && !targetingKey.trim().isEmpty()) {
55-
attributes.put(TARGETING_KEY, new Value(targetingKey));
55+
final Map<String, Value> actualAttribs = new HashMap<>(attributes);
56+
actualAttribs.put(TARGETING_KEY, new Value(targetingKey));
57+
this.structure = new ImmutableStructure(actualAttribs);
58+
} else {
59+
this.structure = new ImmutableStructure(attributes);
5660
}
57-
this.structure = new ImmutableStructure(attributes);
5861
}
5962

6063
/**

src/main/java/dev/openfeature/sdk/MutableContext.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public MutableContext(Map<String, Value> attributes) {
4242
* @param attributes evaluation context attributes
4343
*/
4444
public MutableContext(String targetingKey, Map<String, Value> attributes) {
45+
this.structure = new MutableStructure(attributes);
4546
if (targetingKey != null && !targetingKey.trim().isEmpty()) {
46-
attributes.put(TARGETING_KEY, new Value(targetingKey));
47+
this.structure.attributes.put(TARGETING_KEY, new Value(targetingKey));
4748
}
48-
this.structure = new MutableStructure(attributes);
4949
}
5050

5151
// override @Delegate methods so that we can use "add" methods and still return MutableContext, not Structure

src/test/java/dev/openfeature/sdk/ImmutableContextTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dev.openfeature.sdk;
22

3+
import java.util.Collections;
4+
import java.util.Map;
35
import org.junit.jupiter.api.DisplayName;
46
import org.junit.jupiter.api.Test;
57

@@ -12,6 +14,17 @@
1214
import static org.junit.jupiter.api.Assertions.assertTrue;
1315

1416
class ImmutableContextTest {
17+
@DisplayName("attributes unable to allow mutation should not affect the immutable context")
18+
@Test
19+
void shouldNotAttemptToModifyAttributesForImmutableContext() {
20+
final Map<String, Value> attributes = new HashMap<>();
21+
attributes.put("key1", new Value("val1"));
22+
attributes.put("key2", new Value("val2"));
23+
// should check the usage of Map.of() which is a more likely use case, but that API isn't available in Java 8
24+
EvaluationContext ctx = new ImmutableContext("targeting key", Collections.unmodifiableMap(attributes));
25+
attributes.put("key3", new Value("val3"));
26+
assertArrayEquals(new Object[]{"key1", "key2", TARGETING_KEY}, ctx.keySet().toArray());
27+
}
1528

1629
@DisplayName("attributes mutation should not affect the immutable context")
1730
@Test

src/test/java/dev/openfeature/sdk/MutableContextTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.junit.jupiter.api.DisplayName;
44
import org.junit.jupiter.api.Test;
55

6+
import java.util.Collections;
67
import java.util.HashMap;
8+
import java.util.Map;
79

810
import static dev.openfeature.sdk.EvaluationContext.TARGETING_KEY;
911
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
@@ -12,6 +14,18 @@
1214

1315
class MutableContextTest {
1416

17+
@DisplayName("attributes unable to allow mutation should not affect the Mutable context")
18+
@Test
19+
void shouldNotAttemptToModifyAttributesForMutableContext() {
20+
final Map<String, Value> attributes = new HashMap<>();
21+
attributes.put("key1", new Value("val1"));
22+
attributes.put("key2", new Value("val2"));
23+
// should check the usage of Map.of() which is a more likely use case, but that API isn't available in Java 8
24+
EvaluationContext ctx = new MutableContext("targeting key", Collections.unmodifiableMap(attributes));
25+
attributes.put("key3", new Value("val3"));
26+
assertArrayEquals(new Object[]{"key1", "key2", TARGETING_KEY}, ctx.keySet().toArray());
27+
}
28+
1529
@DisplayName("targeting key should be changed from the overriding context")
1630
@Test
1731
void shouldChangeTargetingKeyFromOverridingContext() {

0 commit comments

Comments
 (0)