Skip to content

String property deserializes null as "null" for JsonTypeInfo.As.EXISTING_PROPERTY #3271

@jonc2

Description

@jonc2

Describe the bug
When using JsonTypeInfo.As.EXISTING_PROPERTY to deserialize String properties, a null value is being deserialized as "null"

Version information
2.12.2

To Reproduce
If you have a way to reproduce this with:

public class ExistingPropertyShouldBeNullTest {

    @JsonTypeInfo(use = Id.NAME, include = As.EXISTING_PROPERTY, visible = true, property = "type", defaultImpl = DefaultShape.class)
    @JsonSubTypes({@Type(value = Square.class, name = "square")})
    static abstract class Shape {

        public String type;

        public String getType() { return this.type; }

        public void setType(String type) { this.type = type; }
    }

    static class Square extends Shape {}

    static class DefaultShape extends Shape {}

    private final ObjectMapper MAPPER = new ObjectMapper();

    @Test
    public void testDeserializationWithValidType() throws Exception {
        Shape deserShape = MAPPER.readValue("{\"type\":\"square\"}", Shape.class);
        assertEquals("square", deserShape.getType());
    }

    @Test
    public void testDeserializationWithInvalidType() throws Exception {
        Shape deserShape = MAPPER.readValue("{\"type\":\"invalid\"}", Shape.class);
        assertEquals("invalid", deserShape.getType());
    }

    @Test
    public void testDeserializationNull() throws Exception {
        Shape deserShape = MAPPER.readValue("{\"type\":null}", Shape.class);
        assertNull(deserShape.getType()); // error: "expected null, but was:<null>"
    }
}

Expected behavior
The String property should be deserialized to a literal null when null is passed in

Additional context
Similar to #3008, but using EXISTING_PROPERTY instead. Abstraction is also used in the above code example

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions