jackson-dataformat-xml icon indicating copy to clipboard operation
jackson-dataformat-xml copied to clipboard

Two wrapped lists with items of same name

Open TeemuStenhammar opened this issue 8 years ago • 5 comments

Hello!

Please, consider following test case:

    /** Two lists */
    public static final String TEST_TWO_LISTS =
            "  <Wrapper>" +
            "    <ones> " +
            "      <name>item0</name>" +
            "      <name>item1</name>" +
            "    </ones>" +
            "    <twos>" +
            "      <name>item2</name>" +
            "      <name>item3</name>" +
            "    </twos>" +
            "  </Wrapper>";

    static class Wrapper {
        @JacksonXmlElementWrapper(localName = "ones")
        @JacksonXmlProperty(localName = "name")
        public List<String> ones;

        @JacksonXmlElementWrapper(localName = "twos")
        @JacksonXmlProperty(localName = "name")
        public List<String> twos;
    }

    @Test
    public void shouldHandleNestedListsWithObjects() throws IOException {
        ObjectMapper xmlMapper = new XmlMapper();

        Wrapper wrapper = xmlMapper.readValue(TEST_TWO_LISTS, Wrapper.class);
        assertNotNull(wrapper);
    }

This test fails with error: com.fasterxml.jackson.databind.JsonMappingException: Multiple fields representing property "name"

Is this a known is a known issue and may I handle this situation without need for two extra classes (Ones and Twos)?

TeemuStenhammar avatar Apr 21 '16 12:04 TeemuStenhammar

@TeemuStenhammar Unfortunately this is a known problem. There should not be such collision, given that wrappers differ, but due to internal limitations (based on databind in JSON not using wrappers) this exists. I think there is at least one existing issue report (maybe #27). It is not clear as to what would be the easiest way to resolve this, which is why there is no solution yet.

cowtowncoder avatar Apr 21 '16 14:04 cowtowncoder

@cowtowncoder Ok, I'll manage then with extra classes. Thank you for the reply.

However, I have another issue that is actually blocking my progress now totally. I am just creating an issue about that and would really appreciate any help in either fixing or circumventing it.

Should I close this issue now that it is duplicate?

TeemuStenhammar avatar Apr 21 '16 15:04 TeemuStenhammar

@TeemuStenhammar let's leave this open for now; I can close this (or other issue(s)) depending on which has the best description.

cowtowncoder avatar Apr 21 '16 17:04 cowtowncoder

Would like to know what the suggested workaround is. Should I just always wrap list properties in a class that contains just the list?

RikkiGibson avatar Jan 26 '18 19:01 RikkiGibson

I come back one year later. :) Is a fix has been done for this issue ?

julienBaleizao avatar Dec 20 '18 14:12 julienBaleizao