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

Bug 325897

Summary: JAXB: Issues with schema gen dealing with anonymous complex types and namespaces
Product: z_Archived Reporter: David McCann <david.mccann>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Proposed fix
none
Supporting test cases none

Description David McCann CLA 2010-09-21 15:34:20 EDT
Given the following:

  TypeMappingInfo t1 = new TypeMappingInfo();
  t1.setAnnotations(new Annotation[0]);
  t1.setType(Process.class);
  t1.setElementScope(TypeMappingInfo.ElementScope.Global);
  t1.setXmlTagName(new QName("http://xmlns.oracle.com/Test", "process"));
    
  TypeMappingInfo[] types = { t1 };
  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put(JAXBContextFactory.DEFAULT_TARGET_NAMESPACE_KEY,
     "http://xmlns.oracle.com/Test/types");
  JAXBContext cxt = JAXBContextFactory.createContext(types, properties, 
     Thread.currentThread().getContextClassLoader());

  MySchemaOutputResolver mysr = new MySchemaOutputResolver();
  cxt.generateSchema(mysr);

Where Process is:

  @XmlType(name = "")
  @XmlRootElement(name = "process")
  public class Process {
      @XmlElement(required = true)
      protected String input;
  }

We get two schemas generated as expected, but the schema generated for the default target namespace is incorrect:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:ns1="http://xmlns.oracle.com/Test/types" 
      xmlns:ns0="http://xmlns.oracle.com/Test" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://xmlns.oracle.com/Test">
  <xsd:import schemaLocation="schema0.xsd" 
      namespace="http://xmlns.oracle.com/Test/types"/>
  <xsd:element name="process" type="ns1:null"/>
</xsd:schema>

We shouldn't be trying to do an import and reference global types in the imported schema, because there aren't any.  We should detect that there is no type, and generate an anonymous complex type, i.e.

<xsd:schema xmlns:ns0="http://xmlns.oracle.com/Test/types" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://xmlns.oracle.com/Test/types">
   <xsd:element name="process">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="input" type="xsd:string"/>
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>
Comment 1 David McCann CLA 2010-09-21 15:36:49 EDT
Created attachment 179337 [details]
Proposed fix
Comment 2 David McCann CLA 2010-09-21 15:37:07 EDT
Created attachment 179338 [details]
Supporting test cases
Comment 3 David McCann CLA 2010-09-22 10:27:41 EDT
reviewed by: matt.macivor@oracle.com
tests: jaxb/schemagen/anonymoustype/AnonymousTypeTestCases; all unit tests pass as expected
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:26:07 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink