jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

@JsonUnwrapped and JsonTypeInfo.As.EXTERNAL_PROPERTY in the same bean doesn't work

Open tlevavasseur-innovantic opened this issue 7 years ago • 6 comments

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

tlevavasseur-innovantic avatar May 16 '18 13:05 tlevavasseur-innovantic

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.

cowtowncoder avatar May 18 '18 18:05 cowtowncoder

I just fell on this bug 7 years later. I'm using the current latest v2.19.2.

vellotis avatar Jul 23 '25 08:07 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

vellotis avatar Jul 23 '25 09:07 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

vellotis avatar Jul 23 '25 11:07 vellotis

Oh wow still on going. Will see what we can do.

JooHyukKim avatar Jul 23 '25 22:07 JooHyukKim

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.

cowtowncoder avatar Jul 24 '25 20:07 cowtowncoder