Skip to content
Open
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
3 changes: 3 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,9 @@ Martin Mirchev (@Marti2203)
* Reported, fixed #6443: JDK deserialization of a `JsonNode` with a negative length prefix
throws `NegativeArraySizeException` instead of an `IOException`
[3.3.1]
* Reported, fixed #6469: `POJONode.asDecimalOpt()` throws NPE for a wrapped NaN (and
`asDecimal()` variants return `null`)
[3.4.0]

@essiembre
* Reported #6275: Regression in 3.2: per-type `@JsonInclude(NON_DEFAULT)` no longer
Expand Down
3 changes: 3 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Versions: 3.x (for earlier see VERSION-2.x)
#6457: Add `JacksonModule.canAutoRegister()` to allow modules to opt out of
auto-registration
(fix by @cowtowncoder, w/ Claude code)
#6469: `POJONode.asDecimalOpt()` throws NPE for a wrapped NaN (and `asDecimal()`
variants return `null`)
(reported, fixed by @Marti2203)

3.3.1 (not yet released)

Expand Down
17 changes: 11 additions & 6 deletions src/main/java/tools/jackson/databind/node/POJONode.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,12 @@ public BigDecimal asDecimal() {
super.asDecimal();
}

// Then, extract from Number
return _extractAsBigDecimal();
// Then, extract from Number (`null` for NaN and infinities, as with `DoubleNode`)
BigDecimal dec = _extractAsBigDecimal();
if (dec == null) {
return _reportBigDecimalConversionNaNFail("asDecimal()");
}
return dec;
}

@Override
Expand All @@ -483,8 +487,9 @@ public BigDecimal asDecimal(BigDecimal defaultValue) {
return defaultValue;
}

// Then, extract from Number
return _extractAsBigDecimal();
// Then, extract from Number; default for NaN and infinities
BigDecimal dec = _extractAsBigDecimal();
return (dec == null) ? defaultValue : dec;
}

@Override
Expand All @@ -494,8 +499,8 @@ public Optional<BigDecimal> asDecimalOpt() {
return Optional.empty();
}

// Then, extract from Number
return Optional.of(_extractAsBigDecimal());
// Then, extract from Number; empty for NaN and infinities
return Optional.ofNullable(_extractAsBigDecimal());
}

// extract Long from Number, or return null if range check fails
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/tools/jackson/databind/node/POJONodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,18 @@ public void testAsDecimalOpt() {
assertThat(new POJONode(new Data()).asDecimalOpt()).isNotPresent();
}


// NaN and infinities wrapped in a POJONode convert to BigDecimal as DoubleNode's do:
// asDecimalOpt() used to throw NullPointerException
@Test
public void testDecimalAccessorsForNonFinite()
{
for (double d : new double[] { Double.NaN, Double.POSITIVE_INFINITY }) {
POJONode n = new POJONode(d);
assertEquals(Optional.empty(), n.asDecimalOpt());
assertEquals(BigDecimal.ONE, n.asDecimal(BigDecimal.ONE));
assertThrows(JsonNodeException.class, () -> n.asDecimal());
}
assertEquals(Optional.of(BigDecimal.valueOf(2.5)), new POJONode(2.5).asDecimalOpt());
}
}
Loading