Skip to content

Collection values overwritten if item elements are not contiguous (mixed content) #363

Open
@vchimishuk

Description

@vchimishuk

Looks like we have a bug in the parser. Parsing next simple document leads to data loss.

<Data>
    <foo>foo1</foo>
    <foo>foo2</foo>
    <bar>bar1</bar>
    <foo>foo3</foo>
    <foo>foo4</foo>
</Data>

Expected result: foo java-property contains list of 4 values.
Actual result: foo java-property contains list of 2.

Setter for foo property is called two times for every tags group: foo1, foo2 and foo3, foo4. Second setter call overrides data gathered by the first call. As result parsing result contains foo list as foo3, foo4 instead of expected foo1, foo2, foo3, foo4.
Here is a java-code to reproduce the issue.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import java.util.List;

@JacksonXmlRootElement(localName = "data")
class Data {
    @JacksonXmlCData
    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty
    private List<String> foo;

    @JacksonXmlCData
    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty
    private List<String> bar;

    public List<String> getFoo() {
        return foo;
    }

    public void setFoo(List<String> foo) {
        this.foo = foo;
    }

    public List<String> getBar() {
        return bar;
    }

    public void setBar(List<String> bar) {
        this.bar = bar;
    }
}

public class Foo {
    public static void main(String[] args) throws JsonProcessingException {
        String xml = ""
                + "<Data>"
                + "    <foo>foo1</foo>"
                + "    <foo>foo2</foo>"
                + "    <bar>bar1</bar>"
                + "    <foo>foo3</foo>"
                + "    <foo>foo4</foo>"
                + "</Data>";

        XmlMapper m = new XmlMapper();
        Data data = m.readValue(xml, Data.class);

        System.err.println(data.getFoo()); // Expected ["foo1", "foo2", "foo3", "foo4"] but ["foo3", "foo4"] given.
    }
}

jackson-dataformat-xml version: 2.10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    mixed-contentIssue related to XML mixed contentwill-not-fixIssue for which there is no plan to fix as described or requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions