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

`@JacksonXmlElementWrapper` ignored on `Stream`

Open ndionisi opened this issue 6 years ago • 3 comments

jackson-dataformat-xml version: 2.9.8

When using an XMLMapper with Jdk8Module to serialize Streams, the serializer seems to ignore annotation @JacksonXmlElementWrapper placed on a Stream. For example, if I try to serialize this DTO as XML:

public class StreamBasedDto {
        private Stream<String> data;

        @JacksonXmlElementWrapper(localName = "elements")
        @JacksonXmlProperty(localName = "element")
        public Stream<String> getData() {
            return data;
        }

        public void setData(Stream<String> data) {
            this.data = data;
        }
    }

I get the following result:

<StreamBasedDto>
    <element>foo</element>
    <element>bar</element>
</StreamBasedDto>

whereas I was expecting the following:

<StreamBasedDto>
    <elements>
        <element>foo</element>
        <element>bar</element>
    </elements>
</StreamBasedDto>

You can find a Java project reproducing the issue here: https://github.com/ndionisi/jackson-xml-element-wrapper-stream. The project tests that @JacksonXmlElementWrapper is properly taken into account when serializing List, but not when serializing Stream.

ndionisi avatar Feb 05 '19 09:02 ndionisi

I think the problem is that special handling via XmlBeanSerializerModifier checks for TypeUtil.isIndexedType(), and as things are, Stream is not considered container type. Same would be true for things like Iterator too.

In theory this could be improved, but one nasty problem wrt Jackson 2.x is that there can not be direct dependency to Java 8 types. Java 8 module could provide metadata, but only thing it could do would be to add TypeModifier to make Stream JavaType be a container type... which might have side effects.

With Jackson 3.0 this could be resolved, however.

cowtowncoder avatar Feb 06 '19 05:02 cowtowncoder

Note: similar to #302 (possibly can fix both with one patch)

cowtowncoder avatar Oct 03 '19 20:10 cowtowncoder

Realized that support probably requires upgrade of JDK baseline for XML module to be Java 8 -- something that is probably reasonable for 2.11, but can not be done in a patch for 2.10.

cowtowncoder avatar Oct 03 '19 20:10 cowtowncoder