FasterXML / jackson-databind

General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Apache License 2.0
3.53k stars 1.38k forks source link

Deduction-based deserialization fails with List #4770

Closed josevsgeon closed 3 weeks ago

josevsgeon commented 3 weeks ago

Search before asking

Describe the bug

When using Jackson’s @JsonTypeInfo with Id.DEDUCTION for polymorphic deserialization, the deduction mechanism fails to correctly identify subtypes within a List field. Specifically, if a class contains a List of an interface type (e.g., List<BaseType>), and multiple subtypes of the interface are expected within the list, Jackson fails to deduce the individual type of each element, resulting in an InvalidTypeIdException. This occurs even if the subtypes have unique fields that should allow for distinct deduction.

Version Information

2.17.2

Reproduction

  1. Create an interface (e.g., BaseType) with several distinct implementations (SubTypeA, SubTypeB), each having unique fields.
  2. Create a class (e.g., CompositeType) containing a List<BaseType> field annotated with @JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION).
  3. Attempt to deserialize JSON containing multiple BaseType implementations in the list, where each object has unique fields relevant to its type.
@JsonTypeInfo(
    use = JsonTypeInfo.Id.DEDUCTION
)
public interface BaseType {
}

public class CompositeType {
    private List<BaseType> elements;
}

public class SubTypeA implements BaseType {
    private String uniqueFieldA;
}

public class SubTypeB implements BaseType {
    private String uniqueFieldB;
}

Expected behavior

Each element within the List should be deduced individually, allowing Jackson to correctly identify and deserialize each element to the appropriate subtype based on its unique fields.

Additional context

No response

cowtowncoder commented 3 weeks ago

Ok we are almost there, but just to make sure reproduction is fully valid, it'd be great to include actual code and input used -- to rule out possibility of misunderstand with description.

josevsgeon commented 3 weeks ago

i tried to create a minimal reproduction test case, but reproduction is a bit inconsistent. sometimes, it works, other times, it fails. I'm now not sure if the problem is with List.

JooHyukKim commented 3 weeks ago

Then let's close the issue for now, re-open later when you get the 100% reproduction?