Skip to content

String property deserializes null as "null" for JsonTypeInfo.As.EXTERNAL_PROPERTY #3008

@cowtowncoder

Description

@cowtowncoder

(note: copied from FasterXML/jackson-module-kotlin#335)

Looks like following test case, first found with Kotlin, fails:

public class ExternalPropertyTypeShouldBeNullTest {

    static class Box {
        public String type;
        public Fruit fruit;

        public Box(@JsonProperty("type") String type,

                   @JsonTypeInfo(use = Id.NAME, include = As.EXTERNAL_PROPERTY, property = "type")
                   @JsonSubTypes({@Type(value = Orange.class, name = "orange")})
                   @JsonProperty("fruit") Fruit fruit) {
            this.type = type;
            this.fruit = fruit;
        }
    }

    interface Fruit{}

    static class Orange implements Fruit {
        public String name;
        public String color;

        public Orange(@JsonProperty("name") String name, @JsonProperty("name") String color) {
            this.name = name;
            this.color = color;
        }
    }

    private final ObjectMapper MAPPER = new ObjectMapper();

    @Test
    public void testDeserializationNull() throws Exception {
        MAPPER.disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY);
        Box deserOrangeBox = MAPPER.readValue("{\"type\":null,\"fruit\":null}}", Box.class);
        assertNull(deserOrangeBox.fruit);
        assertNull(deserOrangeBox.type); // error: "expected null, but was:<null>"
    }
}

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