question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Polymorphic deserialization with `EXTERNAL_PROPERTY` fails if type property not marked as property

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
burembacommented, May 7, 2018

@cowtowncoder The type id may be useful by itself so don’t assume that type id is there just for deserializing another value. For example, here is our use-case: We have reports and each report has a type. Depending on that type, each report has options and we use different classes for different report types. We also have two different endpoints in our API server; list and get. The list reports the report names and types but not options because we just need to list the reports in a table. When the user clicks a report, we fetch the options from the server and return the data to the user.

0reactions
cowtowncodercommented, May 11, 2018

@buremba Ok. In that case I would need a reproduction of the problem (in plain Java) since test for #1341 passes as expected at this point.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson Polymorphic Deserialization via field - Stack Overflow
In this approach, @JsonTypeInfo is set to use type as an external property to determine the right class to map the data property....
Read more >
Inheritance in Jackson | Baeldung
This tutorial will demonstrate how to handle inclusion of subtype metadata and ignoring properties inherited from superclasses with Jackson.
Read more >
BeanDeserializerBase (jackson-databind 2.7.0 API) - FasterXML
Helper method that can be used to see if specified property is annotated to indicate use of a converter for property value (in...
Read more >
Security update for jackson-databind, jackson-dataformats ...
READ_ONLY' fails with collections when a property name is specified + 'BigDecimal' precision not retained for polymorphic deserialization + ...
Read more >
DeserializationFeature (jackson-databind 2.9.8 API) - javadoc.io
If disabled, and if property is NOT marked as required, missing Creator properties are filled with null values provided by deserializer for the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found