Skip to content

Can't deserialize list in JsonSubtype when type property is visible #455

@jimirocks

Description

@jimirocks

Using the latest 2.12.2 version.
Getting com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No fallback setter/field defined for creator property 'ListItem' (through reference chain: cz.smarteon.loxone.system.status.Child["ListItem"]) when parent's @JsonTypeInfo(visible = true).

So it seems it's not possible to have the type property visible, while using collections in subtypes.

I also have more complex scenario, where the deserialization doesn't fail, but the list in subtype is simply not filled (which is even worse). However I wasn't able to make it short enough to include it in report, instead I found this. This could be also related to #426 ? (just a wild guess)

package test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class VisibleTypePropertyTest {

    public static void main(String[] args) throws Exception {
        final ObjectMapper mapper = XmlMapper.builder().defaultUseWrapper(false).build();
        final String parentXml = "<Item type=\"parent\" someProperty=\"someValue\" />";
        final String childXml = "<Item type=\"child\" someProperty=\"someValue2\">" +
                "<ListItem name=\"a\"/><ListItem name=\"b\" />" +
                "</Item>";

        mapper.readValue(parentXml.getBytes(StandardCharsets.UTF_8), Parent.class);

        try {
            mapper.readValue(childXml.getBytes(StandardCharsets.UTF_8), Parent.class);
        } catch (InvalidDefinitionException ex) {
            System.out.println("This is the BUG");
            ex.printStackTrace();
        }
    }
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY,
        property = "type", defaultImpl = Parent.class
        , visible = true // this triggers the BUG
)
@JsonSubTypes({
        @JsonSubTypes.Type(name = "child", value = Child.class)
})
class Parent {
    final String visibleType;
    final String someProperty;

    @JsonCreator
    Parent(@JsonProperty("type") final String visibleType, @JsonProperty("someProperty") final String someProperty) {
        this.visibleType = visibleType;
        this.someProperty = someProperty;
    }
}

class Child extends Parent {

    final List<ChildItem> list;

    @JsonCreator
    Child(@JsonProperty("type") final String visibleType, @JsonProperty("someProperty") final String someProperty,
          @JsonProperty("ListItem") final List<ChildItem> list) {
        super(visibleType, someProperty);
        this.list = list;
    }
}

class ChildItem {
    @JsonCreator
    ChildItem(@JsonProperty("name") final String name) {}
}

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions