Moxy ignores package level xml-accessor-order if xml-accessor-type is FIELD and type is not present in xml-bindings
How to repeat
Two Classes: Foo Bar
In XML bindings file define package level xml-accessor-type="FIELD" xml-accessor-order="ALPHABETICAL"
Type Bar is not present in XML binding file
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="com.test.moxy"
xml-accessor-type="FIELD" xml-accessor-order="ALPHABETICAL"
xml-mapping-metadata-complete="true">
<java-types>
<java-type name="Foo">
<xml-root-element/>
</java-type>
</java-types>
</xml-bindings>
As the result, fields in Foo class are ordered, but fields in Bar class is not:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="foo">
<xsd:sequence>
<xsd:element name="aField" type="xsd:string" minOccurs="0"/>
<xsd:element name="bField" type="xsd:string" minOccurs="0"/>
<xsd:element name="bar" type="bar" minOccurs="0"/>
<xsd:element name="zField" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bar">
<xsd:sequence>
<xsd:element name="zBarField" type="xsd:string" minOccurs="0"/>
<xsd:element name="bBarField" type="xsd:string" minOccurs="0"/>
<xsd:element name="aBarField" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="foo" type="foo"/>
</xsd:schema>
If we do the same using JAXB Java package annotations
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@XmlAccessorType(XmlAccessType.FIELD)
package com.test.moxy;
Fields in both types will be sorted:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="foo">
<xsd:sequence>
<xsd:element name="aField" type="xsd:string" minOccurs="0"/>
<xsd:element name="bField" type="xsd:string" minOccurs="0"/>
<xsd:element name="bar" type="bar" minOccurs="0"/>
<xsd:element name="zField" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bar">
<xsd:sequence>
<xsd:element name="aBarField" type="xsd:string" minOccurs="0"/>
<xsd:element name="bBarField" type="xsd:string" minOccurs="0"/>
<xsd:element name="zBarField" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="foo" type="foo"/>
</xsd:schema>
See attached test project with examples jaxb-access-order.zip
The same problem is repeatable with versions 2.7.11, 3.0.x and 4.0.x
If type Bar is present in XML bindings file or package level access order is not FIELD but PROPERTY, then it works correctly.
Sorry but I don't understand this issue.
In XML binding file is Bar class not specified -> how MOXy should know that ALPHABETICAL access order is wanted there. It's applied to specified classes only.
In case of package annotations there is wider scope to all entities in the package.
In XML binding file Bar class is not specified, but XML binding file contains package level directive to set default access order for package as "ALPHABETICAL":
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="com.test.moxy"
xml-accessor-type="FIELD" xml-accessor-order="ALPHABETICAL"
Similarly as JAXB Java package annotations in package-info.java file:
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@XmlAccessorType(XmlAccessType.FIELD)
package com.test.moxy;
Therefore I am expecting that both these directives will produce the same result, but it is not the case. JAXB Java package annotations in package-info.java file are taken into account and fields are ALPHABETICALLY sorted, but moxy XML package directives are ignored and fields are NOT ALPHABETICALLY sorted.
Also in eclipselink documentation it states that in <xml-bindings> element you can define default xml-accessor-type and default xml-accessor-order:
The root of the XML Bindings document. This is also where you can define top-level properties for your JAXB system, such as the package-name of your classes, specify a default xml-accessor-type, and so on.