Skip to content

@JsonCreator on constructor not compatible with @JsonIdentityInfo, PropertyGenerator #2944

@symposion

Description

@symposion

On Jackson 2.11.3

A bean annotated with @JsonIdentityInfo pointing at an id property will not deserialize correctly using @JsonCreator on its constructor. The code explicitly ignores identity fields when looking for constructor parameters (they are set after creation) but this makes JsonIdentityInfo incompatible with any sort of immutable object that requries all its properties to be set at construction time.

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

public class Test {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new ParameterNamesModule());
        mapper.readValue(JSON, JsonBean.class);
    }


    private static final String JSON = "{\"id\": \"myId\",\"value\": \"myValue\"}";

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    public static class JsonBean {

        private final String id;
        private final String value;

        @JsonCreator
        public JsonBean(String id, String value) {
            System.out.println(id);
            this.id = id;
            this.value = value;
        }

        public String getId() {
            return id;
        }

        public String getValue() {
            return value;
        }
    }

}

I would expect this to print out "myId"; instead it prints "null". Note that you need to compile with -parameters for this to work

Metadata

Metadata

Assignees

No one assigned

    Labels

    has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions