Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 328855

Summary: JAXB: SchemaGen for XmlElementRefs not working properly if type is not explicitly set
Product: z_Archived Reporter: David McCann <david.mccann>
Component: EclipselinkAssignee: Matt MacIvor <matt.macivor>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: david.twelves, eclipselink.oxm-inbox, matt.macivor
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Proposed Fix and Updated Test none

Description David McCann CLA 2010-10-27 12:09:24 EDT
Given the following model class:

@XmlRootElement(name="my-foo")
public class Foo {
    @XmlElementRefs({
        @XmlElementRef(name="a"), 
        @XmlElementRef(name="b")
    })
    public List<Object> stuff;
}

And ObjectFactory:

@javax.xml.bind.annotation.XmlRegistry
public class ObjectFactory {
    @javax.xml.bind.annotation.XmlElementDecl(name="a")
    public javax.xml.bind.JAXBElement<String> createA() {
        return new javax.xml.bind.JAXBElement<String>(
            new javax.xml.namespace.QName("a"), 
            String.class, 
            "");
    }

    @javax.xml.bind.annotation.XmlElementDecl(name="b")
    public javax.xml.bind.JAXBElement<Integer> createB() {
        return new javax.xml.bind.JAXBElement<Integer>(
            new javax.xml.namespace.QName("b"), 
            java.lang.Integer.class, 
            0);
    }
}

We expect the following schema to be generated:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="foo">
      <xsd:sequence>
         <xsd:choice maxOccurs="unbounded">
            <xsd:element ref="a"/>
            <xsd:element ref="b"/>
         </xsd:choice>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="b" type="xsd:int"/>
   <xsd:element name="a" type="xsd:string"/>
   <xsd:element name="my-foo" type="foo"/>
</xsd:schema>

However we now get the following:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="foo">
      <xsd:sequence>
         <xsd:element ref="my-foo" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="b" type="xsd:int"/>
   <xsd:element name="a" type="xsd:string"/>
   <xsd:element name="my-foo" type="foo"/>
</xsd:schema>

Revision 8407 of AnnotationsProcessor seems to work correctly.

Note that the schema is generated correctly if the type is set by either:

    public List<JAXBElement> stuff

or

    @XmlElementRef(name="a" type="JAXBElement.class")

The failing tests are in:
    org.eclipse.persistence.testing.jaxb.externalizedmetadata.xmlelementrefs.XmlElementRefsTestCases.testXmlElementRefsSchemaGen
Comment 1 David McCann CLA 2010-10-27 12:12:38 EDT
(In reply to comment #0)
> Given the following model class:
> 
> @XmlRootElement(name="my-foo")
> public class Foo {
>     @XmlElementRefs({
>         @XmlElementRef(name="a"), 
>         @XmlElementRef(name="b")
>     })
>     public List<Object> stuff;
> }
> 
> And ObjectFactory:
> 
> @javax.xml.bind.annotation.XmlRegistry
> public class ObjectFactory {
>     @javax.xml.bind.annotation.XmlElementDecl(name="a")
>     public javax.xml.bind.JAXBElement<String> createA() {
>         return new javax.xml.bind.JAXBElement<String>(
>             new javax.xml.namespace.QName("a"), 
>             String.class, 
>             "");
>     }
> 
>     @javax.xml.bind.annotation.XmlElementDecl(name="b")
>     public javax.xml.bind.JAXBElement<Integer> createB() {
>         return new javax.xml.bind.JAXBElement<Integer>(
>             new javax.xml.namespace.QName("b"), 
>             java.lang.Integer.class, 
>             0);
>     }
> }
> 
> We expect the following schema to be generated:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>    <xsd:complexType name="foo">
>       <xsd:sequence>
>          <xsd:choice maxOccurs="unbounded">
>             <xsd:element ref="a"/>
>             <xsd:element ref="b"/>
>          </xsd:choice>
>       </xsd:sequence>
>    </xsd:complexType>
>    <xsd:element name="b" type="xsd:int"/>
>    <xsd:element name="a" type="xsd:string"/>
>    <xsd:element name="my-foo" type="foo"/>
> </xsd:schema>
> 
> However we now get the following:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>    <xsd:complexType name="foo">
>       <xsd:sequence>
>          <xsd:element ref="my-foo" minOccurs="0" maxOccurs="unbounded"/>
>       </xsd:sequence>
>    </xsd:complexType>
>    <xsd:element name="b" type="xsd:int"/>
>    <xsd:element name="a" type="xsd:string"/>
>    <xsd:element name="my-foo" type="foo"/>
> </xsd:schema>
> 
> Revision 8407 of AnnotationsProcessor seems to work correctly.
> 
> Note that the schema is generated correctly if the type is set by either:
> 
>     public List<JAXBElement> stuff
> 
> or
> 
>     @XmlElementRef(name="a" type="JAXBElement.class")
> 
> The failing tests are in:
>    
> org.eclipse.persistence.testing.jaxb.externalizedmetadata.xmlelementrefs.XmlElementRefsTestCases.testXmlElementRefsSchemaGen

In order to use the above mentioned test case(s) to verify the fix, 'type="javax.xml.bind.JAXBElement"' will have to be removed from the "a" and "b" element-ref entries in the oxm.xml file.
Comment 2 Matt MacIvor CLA 2011-05-05 15:11:43 EDT
Created attachment 194870 [details]
Proposed Fix and Updated Test
Comment 3 Matt MacIvor CLA 2011-05-05 15:56:25 EDT
Attached patch checked in to SVN.
Reviewed by Blaise Doughan
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:08:45 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink