Skip to content

ObjectMapper.treeToValue() no longer invokes JsonDeserializer.getNullValue() #2972

@andpal

Description

@andpal

Describe the bug
If you invoke ObjectMapper::treeToValue it no longer uses JsonDeserializer::getNullValue on the class' declared deserializer when encountering a NullNode, instead just returning a plain null.

Version information
The regression began in 2.10.0, I think. I have tested 2.12.0 and the issue is present there. The last working release I have tested is 2.9.10.

To Reproduce
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>se.anpal</groupId>
    <artifactId>jackson-mve</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <jackson.version>2.12.0</jackson.version>
        <!-- This is the last version where the test passes -->
        <!--<jackson.version>2.9.10</jackson.version>-->
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.5.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
            </plugin>
        </plugins>
    </build>
</project>

JsonElementTest.java

package se.anpal.jackson.mve;

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

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.node.NullNode;

import org.junit.jupiter.api.Test;

import se.anpal.jackson.mve.JsonElementTest.JsonElement.JsonNull;

public class JsonElementTest {

    @Test
    public void test() throws Exception {
        NullNode input = NullNode.getInstance();
        JsonNull expected = JsonNull.INSTANCE;
        JsonElement actual = new ObjectMapper().treeToValue(input, JsonElement.class);
        assertEquals(expected, actual);
    }

    @JsonDeserialize(using = JsonElement.MyDeserializer.class)
    public abstract static class JsonElement {

        public static class JsonNull extends JsonElement {

            public static final JsonNull INSTANCE = new JsonNull();
        }

        public static final class MyDeserializer extends JsonDeserializer<JsonElement> {

            @Override
            public JsonElement deserialize(JsonParser jp, DeserializationContext dc) {
                throw new UnsupportedOperationException("Not needed for this example");
            }

            @Override
            public JsonElement getNullValue(DeserializationContext ctxt) {
                return JsonNull.INSTANCE;
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions