Skip to content

Jackson gets confused by parent list element #314

@ewirch

Description

@ewirch

Using Jackson 2.9.7, it fails to deserialize xml elements if they are in a specific order. Changing order works around the problem. The 'problematic' elements are in a collection element. Unboxing those works around the problem as well. See this test class:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class JacksonTest {

    private ObjectMapper mapper;

    @Before
    public void setUp() {
        mapper = new XmlMapper(new JacksonXmlModule());
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
    }

    @Test
    public void testOrder1() throws IOException {
        mapper.readValue(order1Xml(), Customer.class);
    }

    @Test
    public void testOrder2() throws IOException {
        mapper.readValue(order2Xml(), Customer.class);
    }

    @Test
    public void testAddressAsParent() throws IOException {
        mapper.readValue(addressAsParentXml(), Address.class);
    }

    @Data
    public static class Customer {
        @JacksonXmlElementWrapper(localName = "Customer", useWrapping = false)
        @JacksonXmlProperty(localName = "Address")
        private final List<Address> address;
    }

    @Data
    public static class Address {
        private final String stateProv;
        private final CountryName countryName;
    }

    @Data
    @NoArgsConstructor
    public static class CountryName {
        private String code;
        @JacksonXmlText
        private String name;
    }

    private String order1Xml() {
        return ""
            + "<Customer>"
            + "  <Address>\n"
            + "    <StateProv StateCode=\"DE-NI\">Niedersachsen</StateProv>\n"
            + "    <CountryName Code=\"DE\">Deutschland</CountryName>\n"
            + "  </Address>\n"
            + "</Customer>"
            ;
    }

    private String order2Xml() {
        return ""
            + "<Customer>"
            + "  <Address>\n"
            + "    <CountryName Code=\"DE\">Deutschland</CountryName>\n"
            + "    <StateProv StateCode=\"DE-NI\">Niedersachsen</StateProv>\n"
            + "  </Address>\n"
            + "</Customer>"
            ;
    }

    private String addressAsParentXml() {
        return ""
            + "  <Address>\n"
            + "    <CountryName Code=\"DE\">Deutschland</CountryName>\n"
            + "    <StateProv StateCode=\"DE-NI\">Niedersachsen</StateProv>\n"
            + "  </Address>\n"
            ;
    }

}

Expected behavior: all tests should pass.

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