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

First attribute of list ignored #47

Closed
dewthefifth opened this issue Nov 24, 2012 · 4 comments
Closed

First attribute of list ignored #47

dewthefifth opened this issue Nov 24, 2012 · 4 comments

Comments

@dewthefifth
Copy link

The unit test below will reproduce the problem. The general problem is that when the XML is deserialized jackson skips the attributes in the first entry of a list. All subsequent entries have their attributes read, and using an element instead of an attribute will work, but the first item in the list has it's attributes ignored.

I tried stepping through the code a bit, and found that the JsonParser tokens seem to go straight from the "wrapper" field to the "a" field without ever stopping at the "id" field. I could be wrong about this though.


import java.io.StringReader;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public class TestParseMarketStat
{
    @Test
    public void testBrokenParser() throws Exception
    {
        String xml = "<response><wrapper><item id=\"1\"><a>x</a><b>y</b></item><item id=\"2\"><a>y</a><b>x</b></item></wrapper></response>";
        XmlMapper mapper = new XmlMapper();
        Response res = mapper.readValue(new StringReader(xml), Response.class);

        System.out.println(new ObjectMapper().writeValueAsString(res));
        Assert.assertNotNull(res.getItems().get(0).getId());
    }

    public static class Response
    {
        @JacksonXmlProperty(localName = "wrapper")
        List<Item> items;

        public List<Item> getItems()
        {
            return items;
        }

        public void setItems(List<Item> items)
        {
            this.items = items;
        }
    }

    public static class Item
    {
        protected String id;
        protected String a;
        protected String b;

        public String getId()
        {
            return id;
        }

        public void setId(String id)
        {
            this.id = id;
        }

        public String getA()
        {
            return a;
        }

        public void setA(String a)
        {
            this.a = a;
        }

        public String getB()
        {
            return b;
        }

        public void setB(String b)
        {
            this.b = b;
        }

    }
}
@cowtowncoder
Copy link
Member

You are probably right, and I am guessing this behavior may be related to a fix made to allow mapping of special case, where String-value elements had "xml:lang" value (included in 2.1). It should obviously not lead to data loss as here, but perhaps does. Or it may be related to other aspects of optional wrapping of array values (and specifically case where there is no wrapping).

@cowtowncoder
Copy link
Member

I can reproduce the issue; adding (failing) unit test. Interestingly, id of second item is handled appropriately, just not the first one.

@cowtowncoder
Copy link
Member

Digging in deeper, problem only occurs if wrapper does not have attributes, but the first value has.
In this case, call to skipAttributes() occurs when pointing to the first value; whereas in the other case it still points to the wrapper element

@cowtowncoder
Copy link
Member

Fixed for 2.2 -- fix itself is simple, so theoretically could backport to 2.1; however, since there could be regression (none of unit test fails, so there shouldn't be, but...), prefer fixing in new minor release.

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