Skip to content

Problem deserializing POJO with unwrapped List, ignorable attribute valueΒ #301

@JulienHoueix

Description

@JulienHoueix

Hello,

I encountered a bug during XML deserialization with Jackson 2.9.6

If I have a parent class that :

  • contains a list of objects with a specific name
  • and also contains another list of different objects which contain a value with that same name

I get the following error when I try to serialize the XML :

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.Double out of START_OBJECT token
at [Source: (StringReader); line: 1, column: 44] (through reference chain: ch.hcuge.deja.drug.client.Parent["CHILDB"]->java.util.ArrayList[0]->ch.hcuge.deja.drug.client.ChildB["MY_PROPERTY"])

But I have this error only if the parent object contains an attribute!

I have a test case that shows the problem :

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import org.junit.Test;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class TestBug {

    // Succeeds
    @Test
    public void testWithoutAttribute() throws Exception {
        StringWriter stringWriter = new StringWriter();
        stringWriter.append("" +
                "<PARENT>" +
                "  <CHILDB>" +
                "    <MY_PROPERTY>12.34</MY_PROPERTY>" +
                "  </CHILDB>" +
                "</PARENT>");
        XmlMapper xmlMapper = new XmlMapper();
        Parent deserializedParent = xmlMapper.readValue(stringWriter.toString(), Parent.class);
    }

    // Fails!
    @Test
    public void testWithAttribute() throws Exception {
        StringWriter stringWriter = new StringWriter();
        stringWriter.append("" +
                "<PARENT>" +
                "  <CHILDB MY_ATTR=\"TEST_VALUE\">" +
                "    <MY_PROPERTY>12.34</MY_PROPERTY>" +
                "  </CHILDB>" +
                "</PARENT>");
        XmlMapper xmlMapper = new XmlMapper();
        Parent deserializedParent = xmlMapper.readValue(stringWriter.toString(), Parent.class);
    }

}

class Root {

    @JacksonXmlProperty(localName = "PARENT")
    public Parent parent;

}

@JsonIgnoreProperties(ignoreUnknown = true)
class Parent {

    @JacksonXmlProperty(localName = "MY_ATTR", isAttribute = true)
    public String myAttribute;

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "MY_PROPERTY")
    public List<ChildA> childrenA = new ArrayList<>();

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "CHILDB")
    public List<ChildB> childrenB = new ArrayList<>();

}

class ChildA {

}

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
class ChildB {

    @JacksonXmlProperty(localName = "MY_PROPERTY")
    public Double value;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions