Skip to content
Draft
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 @@ -428,13 +428,16 @@ private static boolean compareNumber(

/**
* Converts a comparison operand to a double. A {@link Number} converts without allocating. Any
* other value goes through {@link #mapValue} as before: a non-numeric value fails with mapValue's
* exception, and null fails when its null result is unboxed.
* other non-null value goes through {@link #mapValue}: a non-numeric value fails with mapValue's
* exception. A null operand throws a {@link NullPointerException} with a fixed message.
*/
private static double toDouble(final Object value) {
if (value instanceof Number) {
return ((Number) value).doubleValue();
}
if (value == null) {
throw new NullPointerException("Cannot compare a null value as a number");
}
return mapValue(Double.class, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.datadog.openfeature.internal.ufc.Split;
import com.datadog.openfeature.internal.ufc.ValueType;
import com.datadog.openfeature.internal.ufc.Variant;
import dev.openfeature.sdk.ErrorCode;
import dev.openfeature.sdk.MutableContext;
import dev.openfeature.sdk.ProviderEvaluation;
import dev.openfeature.sdk.Value;
Expand Down Expand Up @@ -239,6 +240,29 @@ public Iterator<Object> iterator() {
assertEquals(scan.getErrorMessage(), indexed.getErrorMessage());
}

@TableTest({
"operator | nullFirst",
"ONE_OF | true ",
"ONE_OF | false ",
"NOT_ONE_OF | true ",
"NOT_ONE_OF | false "
})
void preservesNullElementOrder(final ConditionOperator operator, final boolean nullFirst) {
final List<Object> values = nullFirst ? asList(null, "42") : asList("42", null);
if (nullFirst) {
assertEquals(0, MembershipIndex.throwsAt(MembershipIndex.build(values).lookup(42)));
}
for (final boolean indexed : new boolean[] {false, true}) {
final ProviderEvaluation<String> result = evaluate(operator, values, 42, indexed);

assertEquals(nullFirst ? ErrorCode.GENERAL : null, result.getErrorCode());
assertEquals(
nullFirst ? "Cannot compare a null value as a number" : null, result.getErrorMessage());
assertEquals(
!nullFirst && operator == ConditionOperator.ONE_OF ? "in" : "out", result.getValue());
}
}

private void assertSameResult(
final ConditionOperator operator, final List<Object> list, final Object attribute) {
final ProviderEvaluation<String> scan = evaluate(operator, list, attribute, false);
Expand Down Expand Up @@ -284,7 +308,7 @@ private ProviderEvaluation<String> evaluate(
singletonMap("in", new Variant("in", "in")),
singletonList(allocation));
final ServerConfiguration config =
new ServerConfiguration("", "", null, null, singletonMap(FLAG, flag));
new ServerConfiguration("", "", true, null, singletonMap(FLAG, flag));
final MutableContext context =
new MutableContext("user", singletonMap("attr", toValue(attribute)));
return evaluator.evaluate(config, String.class, FLAG, "out", context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import com.datadog.openfeature.ExperimentationOptOutMode;
import com.datadog.openfeature.internal.ufc.Allocation;
Expand Down Expand Up @@ -381,7 +380,7 @@ void reportsNumericConversionErrorsAsMapValueDoes(
}

@Test
void reportsANullListElementAsMapValueDoes() {
void reportsANullListElementWithADeterministicMessage() {
final MutableContext context = new MutableContext("user");
context.add("score", 5);

Expand All @@ -392,10 +391,7 @@ void reportsANullListElementAsMapValueDoes() {
context);

assertEquals(ErrorCode.GENERAL, result.getErrorCode());
final String expectedMessage = mapValueFailure(null).getMessage();
// Before JDK 15 a NullPointerException has no message, so there is nothing to compare.
assumeTrue(expectedMessage != null, "helpful NullPointerException messages are unavailable");
assertEquals(expectedMessage, result.getErrorMessage());
assertEquals("Cannot compare a null value as a number", result.getErrorMessage());
}

@Test
Expand Down
Loading