Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Do not throw null reference exception accessing a missing item. #300

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Set<String> keySet() {
@Override
public Value getValue(String key) {
Value value = this.attributes.get(key);
return value.clone();
return value != null ? value.clone() : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding/fixing/testing this. I don't think we have a similar issue in MutableStructure (the older implementation) because we don't clone the retrieved value.

}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/test/java/dev/openfeature/sdk/ImmutableStructureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
beeme1mr marked this conversation as resolved.
Show resolved Hide resolved
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

class ImmutableStructureTest {
@Test void noArgShouldContainEmptyAttributes() {
Expand Down Expand Up @@ -108,4 +104,11 @@ void ModifyingTheValuesReturnByTheKeySetMethodShouldNotModifyTheUnderlyingImmuta
keys.remove("key1");
assertEquals(2, structure.keySet().size());
}

@Test
void GettingAMissingValueShouldReturnNull() {
ImmutableStructure structure = new ImmutableStructure();
Object value = structure.getValue("missing");
assertNull(value);
}
}