Skip to content

Commit

Permalink
Add test for #4934, minor fix (#4940)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Jan 30, 2025
1 parent eb71d68 commit efe5d07
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ public JsonNode readTree(JsonParser p) throws IOException {
*/
public <T> T readTreeAsValue(JsonNode n, Class<T> targetType) throws IOException
{
if (n == null) {
if (n == null || n.isMissingNode()) {
return null;
}
try (TreeTraversingParser p = _treeAsTokens(n)) {
Expand All @@ -1098,7 +1098,7 @@ public <T> T readTreeAsValue(JsonNode n, Class<T> targetType) throws IOException
*/
public <T> T readTreeAsValue(JsonNode n, JavaType targetType) throws IOException
{
if (n == null) {
if (n == null || n.isMissingNode()) {
return null;
}
try (TreeTraversingParser p = _treeAsTokens(n)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.fasterxml.jackson.databind;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

public class DeserializationContextTest extends DatabindTestUtil
{
private final ObjectMapper MAPPER = newJsonMapper();

static class Bean4934 {
public String value;
}

// [databind#4934]
@Test
public void testTreeAsValueFromNulls() throws Exception
{
final JsonNodeFactory nodeF = MAPPER.getNodeFactory();
try (JsonParser p = MAPPER.createParser("abc")) {
DeserializationContext ctxt = MAPPER.readerFor(String.class).createDeserializationContext(p);

assertNull(ctxt.readTreeAsValue(nodeF.nullNode(), Boolean.class));
assertEquals(Boolean.FALSE, ctxt.readTreeAsValue(nodeF.nullNode(), Boolean.TYPE));

// Only fixed in 2.19:
//assertNull(ctxt.readTreeAsValue(nodeF.nullNode(), Bean4934.class));

}
}

// [databind#4934]
@Test
public void testTreeAsValueFromMissing() throws Exception
{
final JsonNodeFactory nodeF = MAPPER.getNodeFactory();
try (JsonParser p = MAPPER.createParser("abc")) {
DeserializationContext ctxt = MAPPER.readerFor(String.class).createDeserializationContext(p);

assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Boolean.class));
// Absent becomes `null` for now as well
assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Boolean.TYPE));

// Only fixed in 2.19:
//assertNull(ctxt.readTreeAsValue(nodeF.missingNode(), Bean4934.class));
}
}
}

0 comments on commit efe5d07

Please sign in to comment.