Hi, using @JsonUnwrapped
for a propery and @JsonTypeInfo(include = JsonTypeInfo.As.**EXTERNAL_PROPERTY**, ...)
on another property in the same bean make the parser totally ignore the @JsonTypeInfo
property :
public class MainType {
@JsonProperty public String text;
public static class Wrapped { public String wrapped; }
@JsonUnwrapped public Wrapped wrapped;
public static class SubType {
public static class SubA extends SubType {
@JsonProperty public boolean bool;
}
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "subtype")
@JsonSubTypes({ @JsonSubTypes.Type(value = SubType.SubA.class, name = "SubA") })
public SubType sub;
}
{
"text": "this is A",
"wrapped": "yes",
"subtype": "SubA",
"sub": {
"bool": true
}
}
final MainType main = mapper.readValue(json, MainType.class);
assertEquals("this is A", main.text);
assertEquals("yes", main.wrapped.wrapped);
assertTrue(main.sub instanceof MainType.SubType.SubA); // <- fails here
assertEquals(true, ((MainType.SubType.SubA) main.sub).bool);
Note : using @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
in SubType
class (instead of sub
property in MainType
) is ok but I need to have subtype
in the parent class and then @JsonTypeInfo
annotation must be on the sub
property.
Tested with 2.9.5
Comment From: cowtowncoder
Thank you for reporting this, troubleshooting, and especially including version information!
I suspect the difference from behavior stems from slightly different code path taken in presence of handlers needed for unwrapped values. Both approaches should work I think.
Comment From: vellotis
I just fell on this bug 7 years later. I'm using the current latest v2.19.2
.
Comment From: vellotis
It is either one or the other https://github.com/FasterXML/jackson-databind/blob/2.x/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializer.java#L334-L339
Comment From: vellotis
It is a known issue that also a test. https://github.com/FasterXML/jackson-databind/blob/70deb562a742ce36a7fceaa6f3c330ba9334ccbe/src/test/java/com/fasterxml/jackson/databind/tofix/ExternalTypeIdWithUnwrapped2039Test.java#L13
Comment From: JooHyukKim
Oh wow still on going. Will see what we can do.
Comment From: cowtowncoder
EXTERNAL_PROPERTY
handling is quite specialized, as is Unwrapping. So while reproduction is easy, fixing may or may not be.
But of course would be great to resolve.