Deserialization of class inheritance depends on attributes order
See original GitHub issueWhen I try to deserialize this xml, XmlMapper creates empty list of action-elements:
<widget id="4" type="Transition">
<attributes>
<event/>
<action name="a"/>
<action name="b"/>
<code/>
<guard/>
</attributes>
</widget>
But if I put the type attribute on the first position, the list contains 2 elements.
Parent class Widget:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", defaultImpl = TransitionWidget.class)
@JsonSubTypes({
@JsonSubTypes.Type(value = StateWidget.class, name = "State"),
@JsonSubTypes.Type(value = TransitionWidget.class, name = "Transition")
})
public abstract class Widget {
private Integer id;
protected Widget(Integer id) {
this.id = id;
}
@JacksonXmlProperty(isAttribute = true)
public Integer getId() {
return id;
}
}
Child class TransitionWidget:
@JacksonXmlRootElement(localName = "widget")
public class TransitionWidget extends Widget {
private final TransitionAttributes attributes;
@JsonCreator
public TransitionWidget(@JsonProperty("id") Integer id, @JsonProperty("attributes") TransitionAttributes attributes) {
super(id);
this.attributes = attributes;
}
@JacksonXmlProperty(localName = "attributes")
public TransitionAttributes getAttributes() {
return attributes;
}
}
And its inner class:
@JacksonXmlRootElement(localName = "attributes")
public class TransitionAttributes {
private Event event;
private List<Action> actions;
private String code;
private String guard;
@JacksonXmlProperty(localName = "action")
@JacksonXmlElementWrapper(useWrapping = false)
public List<Action> getActions() {
return actions;
}
}
Method “getActions” also returns empty list, when type-attribute of widget is missing while defaultImpl is specified.
This is checked with Jackson 2.8.8 and 2.9.0.pr3.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (8 by maintainers)
Top Results From Across the Web
Deserialization order over the inheritance tree - Stack Overflow
I want to do something in the base class during deserialization, and therefore declared a method marked OnDeserializing , but it's important ...
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 >Serialization and deserialization in Java | Snyk Blog
Let's look at the following example of Java deserialize vulnerability where we serialize an object from a serializable class ValueObject :
Read more >Polymorphism and Inheritance with Jackson - OctoPerf
Learn how to serialize and deserialize polymorphic object trees with Jackson Json Databind. Using practical code examples to make it easy to ...
Read more >Serialization and Deserialization in Java with Example
1. If a parent class has implemented Serializable interface then child class doesn't need to implement it but vice-versa is not true. 2....
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
Figured out a way to solve the specific problem in this case.
@jimirocks Np. Glad I went ahead and problem turned out relatively easy to fix.