base-type not honoured when maxOccurs="unbounded"
please see attached eclipse project
Model: Addressbook -> [0..] Entry -> [1..1] Person (IDREF) Addressbook -> [0..] Entry -> [1..] Address (IDREF) Addressbook -> [0..] Person (ID) Addressbook -> [0..*] Address (ID)
In generated class Entry there is an inconsistency:
@XmlElement(name = "Person", required = true, type = Object.class) @XmlIDREF @XmlSchemaType(name = "IDREF") protected Person person; @XmlElementRef(name = "Address", namespace = "http://xpl.buschmais.com/addressbook", type = JAXBElement.class) protected List
address;it should read: @XmlElementRef(name = "Address", namespace = "http://xpl.buschmais.com/addressbook", type = A d d r e s s .class) protected List
address;when unmashalling, there occurs an ... Exception in thread "main" java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to com.buschmais.xpl.addressbook.Address
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xpl.buschmais.com/addressbook" xmlns:addressbook="http://xpl.buschmais.com/addressbook" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" elementFormDefault="qualified" jaxb:version="2.0"
xs:annotation xs:appinfo <jaxb:globalBindings localScoping="toplevel" /> </xs:appinfo> </xs:annotation> <xs:element name="Addressbook"> xs:complexType xs:sequence <xs:element name="Entry" minOccurs="0" maxOccurs="unbounded"> xs:complexType xs:sequence <xs:element name="Person" type="xs:IDREF" minOccurs="1" maxOccurs="1"> xs:annotation xs:appinfo jaxb:property<jaxb:baseType name="Person" /></jaxb:property> </xs:appinfo> </xs:annotation> </xs:element> <xs:element name="Address" type="xs:IDREF" minOccurs="1" maxOccurs="unbounded"> xs:annotation xs:appinfo jaxb:property<jaxb:baseType name="Address" /></jaxb:property> </xs:appinfo> </xs:annotation> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element ref="addressbook:Person" minOccurs="0" maxOccurs="unbounded" /> <xs:element ref="addressbook:Address" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Person"> xs:complexType xs:sequence <xs:element name="Name" type="xs:string" /> <xs:element name="Age" type="xs:nonNegativeInteger" /> </xs:sequence> <xs:attribute name="id" type="xs:ID" /> </xs:complexType> </xs:element> <xs:element name="Address"> xs:complexType xs:sequence <xs:element name="Street" type="xs:string" /> <xs:element name="City" type="xs:string" /> </xs:sequence> <xs:attribute name="id" type="xs:ID" /> </xs:complexType> </xs:element> </xs:schema>
Environment
Operating System: All Platform: All
Affected Versions
[2.1.3]
Reported by fs5_gmx_net
fs5_gmx_net said: Created an attachment (id=278) eclipse project as test case
snajper said: If it is so, then this would be a bug.
snajper said: I think the issue here is most likely the customization does not get reflected.
File: java_jaxb_references.zip Attached By: fs5_gmx_net
Was assigned to snajper
This issue was imported from java.net JIRA JAXB-580
I am not sure if anyone will see this comment as this issue is fairly old. I am facing it too. The only other mention I could find is this SO question: https://stackoverflow.com/questions/3169650/jaxb-inconsistency-between-1-idref-and-more-than-1 ; but its answer fails to adress the "maxOccurs='unbounded'" part.
Curiously enough, this case (unbounded IDREF element) is mentionned in the javadoc for @XmlIDREF (see example 3 in the following : https://docs.oracle.com/javase/8/docs/api/index.html?javax/xml/bind/annotation/XmlIDREF.html )
So I would expect XJC to compile that doc's example 3's schema to example 3's java class.
I found the reason it doesn't: in RawTypeSetBuilder.XmlTypeRef.canBeType() , XJC forces an ElementReference if there is both IDness and unbounded multiplicity; which in turns forces a JAXBElement to be used at runtime (and at compile time too if you don't try to force a javaType customization). This code was added in commit a6ff8a2 by Kohsuke:
// if we have an adapter or IDness, which requires special
// annotation, and there's more than one element,
// we have no place to put the special annotation, so we need JAXBElement.
if((parent.refs.size()>1 || !parent.mul.isAtMostOnce()) && target.idUse()!=ID.NONE)
I don't see why there would be no place to put the special annotation, especially considering the cited javadoc shows a working Java class with the @XmlIDREF annotation.
Could someone with more JAXB background give a feeling whether there was and still is a good reason for this code? The bug (or at least the behaviour I am not expecting) seems to be fixed if I remove the multiplicity test, and I could certainly submit a pull request, but I'm not 100% sure it wouldn't break other things...