Skip to content

Unexpected attribute at string fields causes extra objects to be created in parent list #390

@d-schmidt

Description

@d-schmidt

Tested with Jackson 2.9.9 and 2.10.3

    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>2.10.3</version>
    </dependency>

So when there is a new attribute at a string field, jackson somehow creates additional objects of the parent list instead of ignoring it.
Expected size of list of One ist 1, but it will be two. Each tag after the broken String causes creation of another 'One'.

package test.xml;
import com.ctc.wstx.stax.*;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.xml.*;
import com.fasterxml.jackson.dataformat.xml.annotation.*;
import org.hamcrest.*;
import org.junit.Test;
import javax.xml.stream.XMLInputFactory;
import java.io.IOException;
import java.util.List;

public class XMLParserTest2 {
    @Test
    public void testXMLAnnotations() throws IOException {
        String xml = "<many>\n"
                + "    <one>\n"
                + "        <value bar=\"baz\">foo</value>\n"
                + "        <another></another>\n"
                + "    </one>\n"
                + "</many>";
        Many many = createXMLMapper().readValue(xml, Many.class);
        MatcherAssert.assertThat(many.ones.size(), CoreMatchers.is(1));
    }

    private static XmlMapper createXMLMapper() {
        XMLInputFactory input = new WstxInputFactory();
        input.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
        input.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);

        XmlMapper xmlMapper = new XmlMapper(new XmlFactory(input, new WstxOutputFactory()));
        xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
                .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
                .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
                .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
                .disable(MapperFeature.AUTO_DETECT_GETTERS)
                .disable(MapperFeature.AUTO_DETECT_SETTERS)
                .enable(MapperFeature.AUTO_DETECT_FIELDS);

        return xmlMapper;
    }

    @JacksonXmlRootElement(localName = "many")
    private static class Many {
        @JacksonXmlProperty(localName = "one")
        @JacksonXmlElementWrapper(useWrapping = false)
        List<One> ones;
    }

    private static class One {
        @JacksonXmlProperty
        String value;
        @JacksonXmlProperty
        String another;
    }
}

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