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: null handling with Structure, Value #663

Merged
merged 10 commits into from
Oct 24, 2023
Merged

Conversation

toddbaert
Copy link
Member

@toddbaert toddbaert commented Oct 23, 2023

  • fix NPEs with Structure.asObjectMap and Structure.convertValue
  • more consistent null handling in structures (empty Value -> null and vice versa)
  • update spotbugs and fix a few new detected issues (finalizer attack)

@toddbaert toddbaert requested a review from a team as a code owner October 23, 2023 20:29
@toddbaert toddbaert changed the title fix: NPE with asObjectMap,convertValue, spotbugs update fix: NPE with Strcuture.asObjectMap,convertValue Oct 23, 2023
@codecov
Copy link

codecov bot commented Oct 23, 2023

Codecov Report

Merging #663 (0bdfd30) into main (75ff31e) will increase coverage by 0.49%.
The diff coverage is 97.29%.

@@             Coverage Diff              @@
##               main     #663      +/-   ##
============================================
+ Coverage     94.47%   94.97%   +0.49%     
- Complexity      362      365       +3     
============================================
  Files            32       33       +1     
  Lines           851      856       +5     
  Branches         51       52       +1     
============================================
+ Hits            804      813       +9     
+ Misses           24       22       -2     
+ Partials         23       21       -2     
Flag Coverage Δ
unittests 94.97% <97.29%> (+0.49%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...in/java/dev/openfeature/sdk/AbstractStructure.java 100.00% <100.00%> (ø)
...n/java/dev/openfeature/sdk/ImmutableStructure.java 100.00% <100.00%> (ø)
...ain/java/dev/openfeature/sdk/MutableStructure.java 95.65% <100.00%> (-1.02%) ⬇️
src/main/java/dev/openfeature/sdk/Value.java 92.72% <100.00%> (+2.63%) ⬆️
src/main/java/dev/openfeature/sdk/Structure.java 86.66% <88.88%> (-1.71%) ⬇️

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

spotbugs-exclusions.xml Outdated Show resolved Hide resolved
spotbugs-exclusions.xml Outdated Show resolved Hide resolved
spotbugs-exclusions.xml Outdated Show resolved Hide resolved
spotbugs-exclusions.xml Outdated Show resolved Hide resolved
toddbaert and others added 4 commits October 23, 2023 17:01
Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
Co-authored-by: Giovanni Liva <giovanni.liva@dynatrace.com>
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
@@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The 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) Values. I think that's wrong. With this change, all nulls in maps are converted to null-containing Values and vice-versa.

Comment on lines +25 to +33
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);
Copy link
Member Author

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 and ImmutableContext, so I've extracted it to an abstract.

@toddbaert toddbaert changed the title fix: NPE with Strcuture.asObjectMap,convertValue fix: null handling with Structure, Value Oct 24, 2023
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
@@ -98,6 +98,20 @@ void mapToStructureTest() {
assertEquals(new Value(Instant.ofEpochSecond(0)), res.getValue("Instant"));
assertEquals(new HashMap<>(), res.getValue("Map").asStructure().asMap());
assertEquals(new Value(immutableContext), res.getValue("ImmutableContext"));
assertNull(res.getValue("nullKey"));
assertEquals(new Value(), res.getValue("nullKey"));
Copy link
Member Author

Choose a reason for hiding this comment

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

Modified assertion associated with this change.

Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
@sonarcloud
Copy link

sonarcloud bot commented Oct 24, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 3 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@toddbaert toddbaert merged commit 3ab330a into main Oct 24, 2023
9 checks passed
@toddbaert toddbaert deleted the fix/npe-asObjectMap branch October 24, 2023 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants