Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jaxb generated class used JAXBElement instead of specified type

This question has been asked in various guises - but I feel there is still room to catalogue this further.

I have an xsd with two element definitions

<xs:complexType name="elementA">
   <xs:sequence>
      <xs:element name="date" type="xs:string" minOccurs="0"/>
      <xs:element name="lastXdigits" type="xs:string" nillable="true" minOccurs="0"/>
   </xs:sequence>
</xs:complexType>

This generates:

protected String date;
@XmlElementRef(name = "lastXdigits", namespace = "http://xxxxxxx", type = JAXBElement.class)
protected JAXBElement<String> lastXDigits;

Changing the xsd to:

<xs:element name="lastXdigits" type="xs:string" nillable="true" minOccurs="1"/>

results in:

protected String date;
@XmlElement(name = "lastXdigits", required = true, nillable = true)
protected String lastXDigits;

and using:

<xs:element name="lastXdigits" type="xs:string" minOccurs="0"/>

results in:

protected String date;
@XmlElement(name = "lastXdigits")
protected String lastXDigits;

This seems extremely odd to me. Why is lastXDigits generated as a JAXBElement type in the first case and why does a String type not suffice in all cases? Also, why should jaxb treat these two elements differently when their definition apart from name are identical?

I'm using a jaxb-xjc ant task from jaxb 2.0.5.

Does this look suspicious or is there good justification for these differences?

like image 955
Ellis Avatar asked Dec 02 '10 16:12

Ellis


1 Answers

If nillable="true" together with minOccurs="0" it is impossible to represent the values with just a String. What would null mean? Does it mean that it was null, or that it occurred 0 times? But I guess that is somewhat moot when it comes to just a String.

Compare with empty list. A null list is certainly different to an empty one.

like image 189
Martin Algesten Avatar answered Oct 20 '22 03:10

Martin Algesten