Skip to content

Commit b325a9c

Browse files
committed
Merge branch '2.19'
2 parents 97dd099 + 7664e53 commit b325a9c

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

release-notes/CREDITS-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -1904,3 +1904,8 @@ Konstantin Maliuga (@badoken)
19041904
Lars Benedetto (@lbenedetto)
19051905
* Contributed #4676: Support other enum naming strategies than camelCase
19061906
(2.19.0)
1907+
1908+
Floris Westerman (@FWest98)
1909+
* Reported #4934: `DeserializationContext.readTreeAsValue()` handles null nodes
1910+
differently from `ObjectMapper.treeToValue()`
1911+
(2.19.0)

release-notes/VERSION-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Project: jackson-databind
4343
#4869: Add `JsonNode.values()` to replace `elements()`
4444
#4896: Coercion shouldn't be necessary for Enums specifying an empty string
4545
(reported by @joaocanaverde-blue)
46+
#4934: `DeserializationContext.readTreeAsValue()` handles null nodes
47+
differently from `ObjectMapper.treeToValue()`
48+
(reported by Floris W)
4649
4750
2.18.3 (not yet released)
4851

src/main/java/tools/jackson/databind/DeserializationContext.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,19 @@ public <T> T readValue(JsonParser p, JavaType type) throws JacksonException {
400400
reportBadDefinition(type,
401401
"Could not find `ValueDeserializer` for type "+ClassUtil.getTypeDescription(type));
402402
}
403-
return (T) deser.deserialize(p, this);
403+
return (T) _readValue(p, deser);
404+
}
405+
406+
/**
407+
* Helper method that should handle special cases for deserialization; most
408+
* notably handling {@code null} (and possibly absent values).
409+
*/
410+
private Object _readValue(JsonParser p, ValueDeserializer<Object> deser) throws JacksonException
411+
{
412+
if (p.hasToken(JsonToken.VALUE_NULL)) {
413+
return deser.getNullValue(this);
414+
}
415+
return deser.deserialize(p, this);
404416
}
405417

406418
/*
@@ -1106,7 +1118,7 @@ public <T> T readPropertyValue(JsonParser p, BeanProperty prop, JavaType type)
11061118
"Could not find `ValueDeserializer` for type %s (via property %s)",
11071119
ClassUtil.getTypeDescription(type), ClassUtil.nameOf(prop)));
11081120
}
1109-
return (T) deser.deserialize(p, this);
1121+
return (T) _readValue(p, deser);
11101122
}
11111123

11121124
/**
@@ -1169,7 +1181,7 @@ private TreeTraversingParser _treeAsTokens(JsonNode n)
11691181
p.nextToken();
11701182
return p;
11711183
}
1172-
1184+
11731185
/*
11741186
/**********************************************************************
11751187
/* Methods for problem handling

src/test/java/tools/jackson/databind/DeserializationContextTest.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
public class DeserializationContextTest extends DatabindTestUtil
1313
{
14-
private final ObjectMapper MAPPER = newJsonMapper();
14+
// Not testing "no nulls for primitives", so
15+
private final ObjectMapper MAPPER = jsonMapperBuilder()
16+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
17+
.build();
1518

1619
static class Bean4934 {
1720
public String value;
@@ -26,14 +29,10 @@ public void testTreeAsValueFromNulls() throws Exception
2629
DeserializationContext ctxt = MAPPER.readerFor(String.class)._deserializationContext(p);
2730

2831
assertNull(ctxt.readTreeAsValue(nodeF.nullNode(), Boolean.class));
32+
assertEquals(Boolean.FALSE, ctxt.readTreeAsValue(nodeF.nullNode(), Boolean.TYPE));
33+
assertNull(ctxt.readTreeAsValue(nodeF.nullNode(), String.class));
2934

30-
// Fails on 3.0, investigate
31-
32-
//assertEquals(Boolean.FALSE, ctxt.readTreeAsValue(nodeF.nullNode(), Boolean.TYPE));
33-
34-
// Only fixed in 2.19:
35-
//assertNull(ctxt.readTreeAsValue(nodeF.nullNode(), Bean4934.class));
36-
35+
assertNull(ctxt.readTreeAsValue(nodeF.nullNode(), Bean4934.class));
3736
}
3837
}
3938

@@ -45,12 +44,12 @@ public void testTreeAsValueFromMissing() throws Exception
4544
try (JsonParser p = MAPPER.createParser("abc")) {
4645
DeserializationContext ctxt = MAPPER.readerFor(String.class)._deserializationContext(p);
4746

48-
assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Boolean.class));
4947
// Absent becomes `null` for now as well
48+
assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Boolean.class));
5049
assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Boolean.TYPE));
50+
assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), String.class));
5151

52-
// Only fixed in 2.19:
53-
//assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Bean4934.class));
52+
assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Bean4934.class));
5453
}
5554
}
5655
}

0 commit comments

Comments
 (0)