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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<!-- used so that lombok can generate suppressions for spotbugs. It needs to find it on the relevant classpath -->
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs</artifactId>
<version>4.7.3</version>
<version>4.8.0</version>
<scope>provided</scope>
</dependency>

Expand Down Expand Up @@ -365,7 +365,7 @@
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs</artifactId>
<version>4.7.3</version>
<version>4.8.0</version>
</dependency>
</dependencies>
<executions>
Expand Down
20 changes: 20 additions & 0 deletions spotbugs-exclusions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@
<Class name="dev.openfeature.sdk.OpenFeatureAPI"/>
<Bug pattern="EI_EXPOSE_REP2"/>
</And>
<And>
Added in spotbugs 4.8.0 - EventProvider shares a name with something from the standard lib (confuising), but change would be breaking
toddbaert marked this conversation as resolved.
Show resolved Hide resolved
<Class name="dev.openfeature.sdk.EventProvider"/>
<Bug pattern="PI_DO_NOT_REUSE_PUBLIC_IDENTIFIERS_CLASS_NAMES"/>
</And>
<And>
Added in spotbugs 4.8.0 - Metadata shares a name with something from the standard lib (confuising), but change would be breaking
toddbaert marked this conversation as resolved.
Show resolved Hide resolved
<Class name="dev.openfeature.sdk.Metadata"/>
<Bug pattern="PI_DO_NOT_REUSE_PUBLIC_IDENTIFIERS_CLASS_NAMES"/>
</And>
<And>
Added in spotbugs 4.8.0 - Reason shares a name with something from the standard lib (confuising), but change would be breaking
toddbaert marked this conversation as resolved.
Show resolved Hide resolved
<Class name="dev.openfeature.sdk.Reason"/>
<Bug pattern="PI_DO_NOT_REUSE_PUBLIC_IDENTIFIERS_CLASS_NAMES"/>
</And>
<And>
Added in spotbugs 4.8.0 - FlagValueType.STRING shares a name with something from the standard lib (confuising), but change would be breaking
toddbaert marked this conversation as resolved.
Show resolved Hide resolved
<Class name="dev.openfeature.sdk.FlagValueType"/>
<Bug pattern="PI_DO_NOT_REUSE_PUBLIC_IDENTIFIERS_FIELD_NAMES"/>
</And>

<!-- Test class that should be excluded -->
<Match>
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/dev/openfeature/sdk/ImmutableStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public Map<String, Object> asObjectMap() {
return attributes
.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> convertValue(getValue(e.getKey()))
));
// 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);
toddbaert marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
toddbaert marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions src/main/java/dev/openfeature/sdk/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public interface Structure {
* @return an Object containing the primitive type.
*/
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();
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/dev/openfeature/sdk/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
*/
@ToString
@EqualsAndHashCode
@SuppressWarnings({"PMD.BeanMembersShouldSerialize", "checkstyle:MissingJavadocType"})
@SuppressWarnings({"PMD.BeanMembersShouldSerialize", "checkstyle:MissingJavadocType", "checkstyle:NoFinalizer"})
public class Value implements Cloneable {

private final Object innerObject;

protected final void finalize() {
// DO NOT REMOVE, spotbugs: CT_CONSTRUCTOR_THROW
}
Kavindu-Dodan marked this conversation as resolved.
Show resolved Hide resolved

/**
* Construct a new null Value.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/dev/openfeature/sdk/StructureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,18 @@ void mapToStructureTest() {
assertEquals(new Value(immutableContext), res.getValue("ImmutableContext"));
assertNull(res.getValue("nullKey"));
}

@Test
void asObjectHandlesNullValue() {
Map<String, Value> map = new HashMap<>();
map.put("null", new Value((String)null));
ImmutableStructure structure = new ImmutableStructure(map);
assertNull(structure.asObjectMap().get("null"));
}

@Test
void convertValueHandlesNullValue() {
ImmutableStructure structure = new ImmutableStructure();
assertNull(structure.convertValue(new Value((String)null)));
}
}
Loading