Skip to content

@JsonCreator not working properly with @JsonIdentityInfo when deserializing a collection of interfaces (immutables.io compatibility)  #1706

Description

@balysv

Tested on 2.8.* and 2.9.0.pr4

Given an interface

@JsonSerialize(as = ImmutableTest.class)
@JsonDeserialize(as = ImmutableTest.class)
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
public interface ITest {
    Integer getId();
    String getName();
}

that has two implementations

public static class ImmutableTest implements ITest {
    private final Integer id;
    private final String name;
    ...
    @JsonCreator(mode = DELEGATING)
    static ImmutableTest fromJson(JsonTest json) {
        return new ImmutableTest(json.id, json.name);
    }
}

@JsonDeserialize
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
public static class JsonTest implements ITest {
    private String name;
    private Integer id;
    ...
}

and a collection of the interface as input for deserialization

[
   { "@id":1, "id":1, "name":"test" }, 
   1
]

the method annotated with @JsonCreator is not being invoked when deserializing the second collection item (referred by @id:1), resulting in an invalid collection - one item being of type ImmutableTest while the other one is JsonTest.

The setup mimics the way immutables.io library generated classes implement jackson compatibility.

Minimal test case that has classes identical to those generated by immutables.io library: https://gist.github.com/balysv/bae96686cbbb745d07b198927f1577f0

I'm not quite sure if the issue is a misuse of jackson annotations by the generated classes or something internal in jackson-databind. Would appreciate hints of how to get the given setup work.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

gen-aiPR created with Generative AI (whole or assisted) (or issue for which gen-ai seems suitable)objectid

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions