Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

@JsonAnyGetter + @JsonTypeInfo combination prevents serialization of properties as elements #117

Closed
gawi opened this issue Jun 12, 2014 · 2 comments
Milestone

Comments

@gawi
Copy link

gawi commented Jun 12, 2014

For the following class:

@JacksonXmlRootElement(localName = "dynaBean", namespace = "")
@JsonTypeInfo(use = Id.NAME, property = "class", include = As.PROPERTY)
public class DynaBean {

    private final Map<String, String> _properties = new TreeMap<String, String>();

    public DynaBean(Map<String, String> values) {
        _properties.putAll(values);
    }

    @JsonAnyGetter
    @JacksonXmlProperty(isAttribute = false)
    public Map<String, String> getProperties() {
        return _properties;
    }
}

For the code:

XmlMapper xmlMapper = new XmlMapper();
Map<String, String> values = new HashMap<String, String>();
values.put("prop1", "val1");
values.put("prop2", "val2");
System.out.println(xmlMapper.writeValueAsString(new DynaBean(values)));

Since we have @JacksonXmlProperty(isAttribute = false), we expect:

<dynaBean xmlns="" class="TestSerializationAttr$DynaBean">
  <prop1>val1</prop1>
  <prop2>val2</prop2>
</dynaBean>

But we have:

<dynaBean xmlns="" class="TestSerializationAttr$DynaBean" prop1="val1" prop2="val2">
</dynaBean>

Test case to add in com.fasterxml.jackson.dataformat.xml.ser.TestSerializationAttr

 @JacksonXmlRootElement(localName = "dynaBean", namespace = "")
    @JsonTypeInfo(use = Id.NAME, property = "class", include = As.PROPERTY)
    public class DynaBean {

        private final Map<String, String> _properties = new TreeMap<String, String>();

        public DynaBean(Map<String, String> values) {
            _properties.putAll(values);
        }

        @JsonAnyGetter
        @JacksonXmlProperty(isAttribute = false)
        public Map<String, String> getProperties() {
            return _properties;
        }
    }

    public void testIssue117() throws JsonProcessingException {
        Map<String, String> values = new HashMap<String, String>();
        values.put("prop1", "val1");
        values.put("prop2", "val2");

        assertEquals("<dynaBean xmlns=\"\" class=\"TestSerializationAttr$DynaBean\"><prop1>val1</prop1><prop2>val2</prop2></dynaBean>",
                     _xmlMapper.writeValueAsString(new DynaBean(values)));
    }

I believe the problem resides somewhere in XmlSerializerBase constructor where the _attributeCount does not take into account the @JsonAnyGetter.

@cowtowncoder
Copy link
Member

Thank you for reporting this. It sounds possible that your suggestion on root cause is correct.

@cowtowncoder cowtowncoder added this to the 2, milestone Jun 12, 2014
@cowtowncoder
Copy link
Member

Underlying reason is actually bit more complex: for @JsonTypeInfo, "write-next-as-attribute" is set, but not cleared before writing out properties. Ideally that should be done; but for now I am just forcing "any-getter" properties to be written out as elements. There probably ought to be a way to allow writing them as attributes; but work-around has to do for now; and specifically fixes unit test shown below.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants