Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson gets confused by parent list element #314

Closed
ewirch opened this issue Oct 8, 2018 · 5 comments
Closed

Jackson gets confused by parent list element #314

ewirch opened this issue Oct 8, 2018 · 5 comments
Milestone

Comments

@ewirch
Copy link

ewirch commented Oct 8, 2018

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.

@JulienHoueix
Copy link

What exception do you get? Could you provide the stacktrace?

@ewirch
Copy link
Author

ewirch commented Oct 31, 2018

Does this question mean that these tests pass for you?

@JulienHoueix
Copy link

No, I was just wondering if I had the same error as you.
By the way, if you remove the attribute "StateCode" in "StateProv", it works.
If, instead of using a String for the property StateProv, you used an object with an attribute and a value, it would probably work.

@cowtowncoder
Copy link
Member

I suspect this might be fixed by changes coming 2.11.1, but need to verify.

@cowtowncoder
Copy link
Member

I can reproduce the issue with 2.11(.1), will add failing test, hoping to investigate more in near future.

cowtowncoder added a commit that referenced this issue May 21, 2020
@cowtowncoder cowtowncoder added this to the 2.12.0 milestone Jun 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants