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

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

Closed
d-schmidt opened this issue Mar 23, 2020 · 1 comment
Milestone

Comments

@d-schmidt
Copy link

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;
    }
}
cowtowncoder added a commit that referenced this issue May 23, 2020
@cowtowncoder
Copy link
Member

I can reproduce this, added failing test, hoping to dig deeper in near future.

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

2 participants