jackson-modules-base icon indicating copy to clipboard operation
jackson-modules-base copied to clipboard

jackson-module-jaxb-annotations: @XmlElements adds unwanted intermediate elements

Open lgayard-brex opened this issue 3 months ago • 1 comments

I'm using Jackson to serialize Jakarta-XML annotated classes to XML.

One class has a List field annotated with @XmlElements. I expected that the object would be serialized into a parent element, and the contents of the list field would be direct children of that element. Instead, Jackson introduces an unwanted intermediary element, so that the contents of the list become grand children.

I have prepared a quick proof-of-concept in this repo: https://github.com/lgayard-brex/jaxb-xml-elements-issue . Also, see example below.

This is on Jackson 2.15.x, but also reproducible on later versions, 2.16.x, 2.17.x, 2.18.x.

Moreover, I see a difference between Glassfish and Jackson for this serialization. Glassfish serializes as per my expectations, while Jackson introduces this unwanted element.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "andOrOrOrEqualto"
})
@XmlRootElement(name = "and")
public class And {

    @XmlElements({
        @XmlElement(name = "and", type = And.class),
        @XmlElement(name = "or", type = Or.class),
        @XmlElement(name = "equalto", type = Equalto.class),
        @XmlElement(name = "notequalto", type = Notequalto.class)
    })
    protected List<Object> andOrOrOrEqualto;

Example.

Expected output

<!-- Expected output -->
<filter>
    <and>
        <notequalto>
            <field>STATUS</field>
            <value>inactive</value>
        </notequalto>
        <equalto>
            <field>NAME</field>
            <value>Swag.com</value>
        </equalto>
    </and>
</filter>

Actual output

<!-- actual output -->
<filter>
  <and>
    <andOrOrOrEqualto>
      <notequalto>
        <field>STATUS</field>
        <value>inactive</value>
      </notequalto>
    </andOrOrOrEqualto>
    <andOrOrOrEqualto>
      <equalto>
        <field>NAME</field>
        <value>Swag.com</value>
      </equalto>
    </andOrOrOrEqualto>
  </and>
</filter>

lgayard-brex avatar Sep 01 '25 12:09 lgayard-brex

Note: implementation of actual handling is in jackson-dataformat-xml (JAXB annotations module just provides information). This sounds like:

https://github.com/FasterXML/jackson-dataformat-xml/issues/197

so basically polymorphic type information would need to be combined with property name. This is not support by Jackson XML module, unfortunately. Underlying challenge is originally due difference b/w JSON and XML logical content models.

cowtowncoder avatar Sep 03 '25 03:09 cowtowncoder