Skip to content

Deserialise from Object (using Creator methods) returns field name instead of value #1853

@askvortcov

Description

@askvortcov

Demonstrated by the following test:

public class JacksonTest {

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Product {

        private final String name;

        @JsonCreator(mode = PROPERTIES)
        public Product(
            @JsonProperty("name") String name
        ) {
            this.name = name;
        }

        @JsonValue
        public String getName(){
            return name;
        }

        @Override
        public String toString() {
            return "|~" + name + "~|";
        }

        @JsonCreator(mode = DELEGATING)
        public static Product from(String name){
            return new Product(name);
        }
    }


    private static final String EXAMPLE_DATA = "{\"name\":\"dummy\",\"other\":{},\"errors\":{}}";
    private static final String TEST_PRODUCT_JSON = "\"testProduct\"";

    private ObjectMapper objectMapper = new ObjectMapper();

    @Test
    public void serialization() throws Exception {
        assertEquals(TEST_PRODUCT_JSON, objectMapper.writeValueAsString(new Product("testProduct")));
    }
    @Test
    public void deserializationFromObject() throws Exception {
        assertEquals("dummy", objectMapper.readValue(EXAMPLE_DATA, Product.class).getName());
    }
    @Test
    public void deserializationFromString() throws Exception {
        assertEquals("testProduct", objectMapper.readValue(TEST_PRODUCT_JSON, Product.class).getName());
    }
}

Which is green with 2.9.1 and fails with 2.9.2:

org.junit.ComparisonFailure: 
Expected :dummy
Actual   :name

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