Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jonc2 opened this issue Sep 6, 2021 · 3 comments
Milestone

Comments

@jonc2
Copy link

jonc2 commented Sep 6, 2021

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

@jonc2 jonc2 added the to-evaluate Issue that has been received but not yet evaluated label Sep 6, 2021
@cowtowncoder
Copy link
Member

One quick note: since 2.12.5 is the latest patch, could you please verify issue still exists (likely does, just want to double-check as there are many fixes in 2.12.3, 4 and 5).

@cowtowncoder cowtowncoder added 2.13 and removed to-evaluate Issue that has been received but not yet evaluated labels Sep 6, 2021
@jonc2
Copy link
Author

jonc2 commented Sep 7, 2021

Verified that the issue still exists on 2.12.5.

Last test testDeserializationNull is still failing.

@cowtowncoder
Copy link
Member

.... and this seems to have caused a regression :-( :-( :-( (see #3313)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants