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.

Deserialization of class inheritance depends on attributes order

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Sep 6, 2019

Figured out a way to solve the specific problem in this case.

0reactions
cowtowncodercommented, Sep 16, 2019

@jimirocks Np. Glad I went ahead and problem turned out relatively easy to fix.

Read more comments on GitHub >

github_iconTop 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 >

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