Skip to content

Polymorphic deserialization with EXTERNAL_PROPERTY fails if type property not marked as propertyΒ #1527

@prtmD

Description

@prtmD

I have class as follows

class XYZ {
    private String type;

    private TypeSpecific typeSpecific;

    public TypeSpecific getTypeSpecific() {
        return typeSpecific;
    }

    @JsonTypeInfo(
		use = JsonTypeInfo.Id.NAME, 
		include = JsonTypeInfo.As.EXTERNAL_PROPERTY, 
		property = "type"			
		)
    @JsonSubTypes({		
		@JsonSubTypes.Type(value = ATypeSpecific.class, name = "a")
    })
    public void setTypeSpecific(TypeSpecific typeSpecific) {
        this.typeSpecific = typeSpecific;
    }
}

Class ATypeSpecific extends TypeSpecific.

I want to deserialize JSON
{"type":"b"}
where typeSpecific will be set as null in object. But I am getting following exception:

com.fasterxml.jackson.databind.JsonMappingException: Missing property 'typeSpecific' for external type id 'type'

How do I deserialize above mentioned JSON into the object? If @JsonTypeInfo requires 'typeSpecific' property during deserialization, then is there a way to add it into JSON before deserialization?

Dependency versions:
jackson-annotations: 2.7.0
jackson-core: 2.7.4
jackson-databind: 2.7.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions