-
Notifications
You must be signed in to change notification settings - Fork 41
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: null handling with Structure, Value #663
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
419b6f7
chore(deps): update dependency com.github.spotbugs:spotbugs to v4.8.0
renovate[bot] e3da4c1
fix: NPE with Strcuture.asObjectMap,convertValue
toddbaert df8498a
Update spotbugs-exclusions.xml
toddbaert 6376415
Update spotbugs-exclusions.xml
toddbaert 5f8528e
Update spotbugs-exclusions.xml
toddbaert a283d7e
Update spotbugs-exclusions.xml
toddbaert 4fcdc38
Update src/main/java/dev/openfeature/sdk/ImmutableStructure.java
toddbaert 2091fb4
fixup: fix all Collections.toMap
toddbaert 42e5913
Update src/test/java/dev/openfeature/sdk/StructureTest.java
toddbaert 0bdfd30
Merge branch 'main' into fix/npe-asObjectMap
toddbaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package dev.openfeature.sdk; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@SuppressWarnings({ "PMD.BeanMembersShouldSerialize", "checkstyle:MissingJavadocType" }) | ||
abstract class AbstractStructure implements Structure { | ||
|
||
protected final Map<String, Value> attributes; | ||
|
||
AbstractStructure() { | ||
this.attributes = new HashMap<>(); | ||
} | ||
|
||
AbstractStructure(Map<String, Value> attributes) { | ||
this.attributes = attributes; | ||
} | ||
|
||
/** | ||
* Get all values as their underlying primitives types. | ||
* | ||
* @return all attributes on the structure into a Map | ||
*/ | ||
@Override | ||
public Map<String, Object> asObjectMap() { | ||
return attributes | ||
.entrySet() | ||
.stream() | ||
// custom collector, workaround for Collectors.toMap in JDK8 | ||
// https://bugs.openjdk.org/browse/JDK-8148463 | ||
.collect(HashMap::new, | ||
(accumulated, entry) -> accumulated.put(entry.getKey(), convertValue(entry.getValue())), | ||
HashMap::putAll); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,12 +48,17 @@ public interface Structure { | |
Map<String, Object> asObjectMap(); | ||
|
||
/** | ||
* convertValue is converting the object type Value in a primitive type. | ||
* Converts the Value into its equivalent primitive type. | ||
* | ||
* @param value - Value object to convert | ||
* @return an Object containing the primitive type. | ||
* @return an Object containing the primitive type, or null. | ||
*/ | ||
default Object convertValue(Value value) { | ||
|
||
if (value == null || value.isNull()) { | ||
return null; | ||
} | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (value.isBoolean()) { | ||
return value.asBoolean(); | ||
} | ||
|
@@ -85,15 +90,14 @@ default Object convertValue(Value value) { | |
if (value.isStructure()) { | ||
Structure s = value.asStructure(); | ||
return s.asMap() | ||
.keySet() | ||
.entrySet() | ||
.stream() | ||
.collect( | ||
Collectors.toMap( | ||
key -> key, | ||
key -> convertValue(s.getValue(key)) | ||
) | ||
); | ||
.collect(HashMap::new, | ||
(accumulated, entry) -> accumulated.put(entry.getKey(), | ||
convertValue(entry.getValue())), | ||
HashMap::putAll); | ||
} | ||
|
||
throw new ValueNotConvertableError(); | ||
} | ||
|
||
|
@@ -134,7 +138,9 @@ default <T extends Structure> Map<String, Value> merge(Function<Map<String, Valu | |
*/ | ||
static Structure mapToStructure(Map<String, Object> map) { | ||
return new MutableStructure(map.entrySet().stream() | ||
.filter(e -> e.getValue() != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were FILTERING null values before. This resulted in maps with null entries not having converted (empty) |
||
.collect(Collectors.toMap(Map.Entry::getKey, e -> objectToValue(e.getValue())))); | ||
.collect(HashMap::new, | ||
(accumulated, entry) -> accumulated.put(entry.getKey(), | ||
objectToValue(entry.getValue())), | ||
HashMap::putAll)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was common to both the
MutableContext
andImmutableContext
, so I've extracted it to an abstract.