Skip to content

Serialization and deserialization issue of sub-types used with JsonTypeInfo.Id.DEDUCTION where sub-types are Object and Array #4772

@Gems

Description

@Gems

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Having the following class structure and annotations the deserialization and serialization doesn't work (but expected):

    @JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
    @JsonSubTypes({@JsonSubTypes.Type(DataObject.class), @JsonSubTypes.Type(DataArray.class)})
    interface Data {
    }

    static class DataItem {
        String id;
    }

    static class DataObject extends DataItem implements Data {
       @JsonCreator
        DataObject(@JsonProperty("id") String id) {
            super(id);
        }
    }

    static class DataArray extends ArrayList<DataItem> implements Data {
        @JsonCreator
        DataArray(Collection<DataItem> items) {
            super(new ArrayList<>(items));
        }
    }

    static class Container {
        Data data;
    }

Version Information

2.18.0
and, I believe, 2.19

Reproduction

Here is a test case that I made passing: https://github.com/Gems/jackson-databind/blob/contrib/2.18-1/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicDeductionObjectVsArray.java

To make Deserialization work, I had to change @JsonTypeInfo annotation to @JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION, defaultImpl = DataArray.class)

To make Serialization work, I had to fix JsonGenerator::writeTypePrefix method (comes from jackson-core) to ignore making WRAPPER_ARRAY in case typeIdDef.id == null.

Expected behavior

I'd expect that with the mentioned definition the deserialization and serialization goes smooth without tweaks that I made, but not sure if this expectation is aligned with the vision for the library design.

Thanks in advance for consideration and comments.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.19Issues planned at 2.19 or laterpolymorphic-deductionIssues related to "Id.DEDUCTION" mode of `@JsonTypeInfo`

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions