Polymorphic deserialization with `EXTERNAL_PROPERTY` fails if type property not marked as property
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:3
- Comments:13 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
@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.