Skip to content

Invalid index error when deserializing unwrapped list element with multiple attributes #101

@mrserverless

Description

@mrserverless

This issue was originally pointed out by https://github.com/ShijunK in #64

I also came across this issue while working with a very complex XML and thought it would be worthwhile raising this on its own. I've re-used ShijunK's example:

public class UnwrappedTest {
    @JacksonXmlRootElement(localName = "root")    
    static class Root {

        @JacksonXmlProperty(localName = "unwrapped")
        @JacksonXmlElementWrapper(useWrapping = false)
        public List<UnwrappedElement> unwrapped;

        @JacksonXmlProperty(localName = "name")
        public String name;

        public static class UnwrappedElement {
            public UnwrappedElement () {}

            public UnwrappedElement (String id, String type) {
                this.id = id;
                this.type = type;
            }

            @JacksonXmlProperty(isAttribute = true)
            public String id;

            @JacksonXmlProperty(isAttribute = true)
            public String type;
        }
    }

    protected final static XmlMapper xmlMapper = new XmlMapper();
    protected final String rootXml = "<root>"  + System.lineSeparator() +
            "  <unwrapped id=\"1\" type=\"string\"/>"  + System.lineSeparator() +
            "  <unwrapped id=\"2\" type=\"string\"/>"  + System.lineSeparator() +
            "  <name>text</name>"  + System.lineSeparator() +
            "</root>";

    protected final Root rootObject = new Root();

    @Before
    public void setUp() {
        xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
        rootObject.unwrapped = Arrays.asList(
                new Root.UnwrappedElement("1", "string"),
                new Root.UnwrappedElement("2", "string")
        );
        rootObject.name = "text";
    }

    @Test
    public void serializeTest () throws Exception {
        //SUCCESS
        assertThat(xmlMapper.writeValueAsString(rootObject)).isEqualTo(rootXml);
    }

    @Test
    public void derializeTest () throws Exception {
        //FAILS with invalid index error
        assertThat(xmlMapper.readValue(rootXml, Root.class)).isEqualsToByComparingFields(rootObject);
    }
}

The exception is:

java.lang.IllegalArgumentException: Invalid index 0; current element has only 0 attributes
at com.ctc.wstx.sr.AttributeCollector.throwIndex(AttributeCollector.java:1037)
at com.ctc.wstx.sr.AttributeCollector.getLocalName(AttributeCollector.java:330)
at com.ctc.wstx.sr.BasicStreamReader.getAttributeLocalName(BasicStreamReader.java:583)
at com.fasterxml.jackson.dataformat.xml.deser.XmlTokenStream._next(XmlTokenStream.java:306)
at com.fasterxml.jackson.dataformat.xml.deser.XmlTokenStream.next(XmlTokenStream.java:162)
at com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser.nextToken(FromXmlParser.java:451)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:238)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer.deserialize(WrapperHandlingDeserializer.java:109)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2098)
at com.mgl.caf.loaniq.api.models.UnwrappedTest.testUnwrappedWithOptionalAttribute(UnwrappedTest.java:44)

I'm using Jackson 2.3.1 with Woodstox 4.2.0. I will attempt to work around this issue by using JAXB in the meanwhile.

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