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

Add JsonNodeFeature.FAIL_ON_NAN_TO_BIG_DECIMAL_COERCION to determine what happens on JsonNode coercion to BigDecimal with NaN #4195

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -61,8 +61,18 @@ public enum JsonNodeFeature implements DatatypeFeature
*
* @since 2.15
*/
STRIP_TRAILING_BIGDECIMAL_ZEROES(true)
;
STRIP_TRAILING_BIGDECIMAL_ZEROES(true),

/**
*
* Feature that determines whether to fail on attempt to coercing {@code NaN} into {@link java.math.BigDecimal}
* because {@link com.fasterxml.jackson.databind.DeserializationFeature#USE_BIG_DECIMAL_FOR_FLOATS} is enabled.
JooHyukKim marked this conversation as resolved.
Show resolved Hide resolved
*<p>
* Default value is {@code true} for backwards-compatibility, but will be changed to {@code false} in 3.0.
*
* @since 2.16
*/
ALLOW_NaN_TO_BIG_DECIMAL_COERCION(true);
JooHyukKim marked this conversation as resolved.
Show resolved Hide resolved

private final static int FEATURE_INDEX = DatatypeFeatures.FEATURE_INDEX_JSON_NODE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,12 @@ protected final JsonNode _fromFloat(JsonParser p, DeserializationContext ctxt,
return _fromBigDecimal(ctxt, nodeFactory, p.getDecimalValue());
}
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
// [databind#4194] Add an option to fail coercing NaN to BigDecimal
// Currently, Jackson 2.x allows such coercion, but Jackson 3.x will not
if (p.isNaN() && !ctxt.isEnabled(JsonNodeFeature.ALLOW_NaN_TO_BIG_DECIMAL_COERCION)) {
ctxt.handleWeirdNumberValue(handledType(), p.getDoubleValue(),
"Cannot convert NaN into BigDecimal");
}
try {
return _fromBigDecimal(ctxt, nodeFactory, p.getDecimalValue());
} catch (NumberFormatException nfe) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.fasterxml.jackson.databind.node;

import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import java.math.BigDecimal;

import com.fasterxml.jackson.core.JsonFactory;
Expand Down Expand Up @@ -39,4 +41,36 @@ public void testBigDecimalCoercionInf() throws Exception
assertTrue("Expected DoubleNode, got: "+jsonNode.getClass().getName()+": "+jsonNode, jsonNode.isDouble());
assertEquals(Double.POSITIVE_INFINITY, jsonNode.doubleValue());
}

// [databind#4194]: should be able to, by configuration, fail coercing NaN to BigDecimal
public void testBigDecimalCoercionNaN() throws Exception
{
_tryBigDecimalCoercionNaNWithOption(true);

try {
_tryBigDecimalCoercionNaNWithOption(false);
fail("Should not pass");
} catch (InvalidFormatException e) {
verifyException(e, "Cannot convert NaN");
}
}

private void _tryBigDecimalCoercionNaNWithOption(boolean isEnabled) throws Exception
{
JsonFactory factory = JsonFactory.builder()
.enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS)
.build();
final ObjectReader reader = new JsonMapper(factory)
.reader()
.with(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);

final String value = "NaN";
// depending on option
final JsonNode jsonNode = isEnabled
? reader.with(JsonNodeFeature.ALLOW_NaN_TO_BIG_DECIMAL_COERCION).readTree(value)
: reader.without(JsonNodeFeature.ALLOW_NaN_TO_BIG_DECIMAL_COERCION).readTree(value);

assertTrue("Expected DoubleNode, got: "+jsonNode.getClass().getName()+": "+jsonNode, jsonNode.isDouble());
assertEquals(Double.NaN, jsonNode.doubleValue());
}
}