Skip to content

Commit efe5d07

Browse files
authored
Add test for #4934, minor fix (#4940)
1 parent eb71d68 commit efe5d07

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ public JsonNode readTree(JsonParser p) throws IOException {
10741074
*/
10751075
public <T> T readTreeAsValue(JsonNode n, Class<T> targetType) throws IOException
10761076
{
1077-
if (n == null) {
1077+
if (n == null || n.isMissingNode()) {
10781078
return null;
10791079
}
10801080
try (TreeTraversingParser p = _treeAsTokens(n)) {
@@ -1098,7 +1098,7 @@ public <T> T readTreeAsValue(JsonNode n, Class<T> targetType) throws IOException
10981098
*/
10991099
public <T> T readTreeAsValue(JsonNode n, JavaType targetType) throws IOException
11001100
{
1101-
if (n == null) {
1101+
if (n == null || n.isMissingNode()) {
11021102
return null;
11031103
}
11041104
try (TreeTraversingParser p = _treeAsTokens(n)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fasterxml.jackson.databind;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
5+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertNull;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
public class DeserializationContextTest extends DatabindTestUtil
13+
{
14+
private final ObjectMapper MAPPER = newJsonMapper();
15+
16+
static class Bean4934 {
17+
public String value;
18+
}
19+
20+
// [databind#4934]
21+
@Test
22+
public void testTreeAsValueFromNulls() throws Exception
23+
{
24+
final JsonNodeFactory nodeF = MAPPER.getNodeFactory();
25+
try (JsonParser p = MAPPER.createParser("abc")) {
26+
DeserializationContext ctxt = MAPPER.readerFor(String.class).createDeserializationContext(p);
27+
28+
assertNull(ctxt.readTreeAsValue(nodeF.nullNode(), Boolean.class));
29+
assertEquals(Boolean.FALSE, ctxt.readTreeAsValue(nodeF.nullNode(), Boolean.TYPE));
30+
31+
// Only fixed in 2.19:
32+
//assertNull(ctxt.readTreeAsValue(nodeF.nullNode(), Bean4934.class));
33+
34+
}
35+
}
36+
37+
// [databind#4934]
38+
@Test
39+
public void testTreeAsValueFromMissing() throws Exception
40+
{
41+
final JsonNodeFactory nodeF = MAPPER.getNodeFactory();
42+
try (JsonParser p = MAPPER.createParser("abc")) {
43+
DeserializationContext ctxt = MAPPER.readerFor(String.class).createDeserializationContext(p);
44+
45+
assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Boolean.class));
46+
// Absent becomes `null` for now as well
47+
assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Boolean.TYPE));
48+
49+
// Only fixed in 2.19:
50+
//assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Bean4934.class));
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)