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

Bug 355261

Summary: Namespace Issue with TypeMappingInfo wrapping Object
Product: z_Archived Reporter: Matt MacIvor <matt.macivor>
Component: EclipselinkAssignee: Denise Smith <denise.mahar>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: david.twelves, denise.mahar
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
New Test reproducing this issue
none
Proposed changes and test
none
Additional changes and tests none

Description Matt MacIvor CLA 2011-08-19 15:23:18 EDT
Given the following Employee class:
@XmlType(name="employee", namespace="someuri")
public class Employee {

    public String name;
    
    public String id;
    
    public boolean equals(Object obj) {
        return name.equals(((Employee)obj).name) && id.equals(((Employee)obj).id);
    }
}

Create a context using TypeMappingInfos, with one based on Object (for Polymorphism)

        TypeMappingInfo[] typeMappingInfos = new TypeMappingInfo[2];
        
        TypeMappingInfo tpi = new TypeMappingInfo();
        tpi.setXmlTagName(new QName("someuri","response"));       
        tpi.setElementScope(ElementScope.Global);
        tpi.setType(Object.class);
        typeMappingInfos[0] = tpi;        
        
        TypeMappingInfo tmi = new TypeMappingInfo();
        tmi.setType(Employee.class);
        tmi.setXmlTagName(new QName("someuri", "employee"));
        tmi.setElementScope(ElementScope.Global);
        typeMappingInfos[1] = tmi;
        
        JAXBContext ctx = JAXBContextFactory.createContext(typeMappingInfos, null, Thread.currentThread().getContextClassLoader());

Create the following JAXBElement, and marshal using the TypeMappingInfo wrapping Object. (The marshal will work fine without the TMI or with the one for Employee, but should also work for the one wrapping object)

        Employee emp = new Employee();
        emp.id = "123";
        emp.name="aaa";
        JAXBElement<Object> elem = new JAXBElement<Object>(new QName("someuri", "response"), Object.class, emp);
        ((JAXBMarshaller)ctx.createMarshaller()).marshal(elem, new StreamResult(System.out), tpi);

Observe the output XML:

<ns1:response xmlns:ns1="someuri" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns0:employee">
   <name>aaa</name>
   <id>123</id>
</ns1:response>

The xsi:type references "ns0:employee", but ns0 is never declared.
Comment 1 Denise Smith CLA 2011-09-16 15:38:55 EDT
Created attachment 203521 [details]
New Test reproducing this issue
Comment 2 Denise Smith CLA 2011-09-19 10:47:16 EDT
Created attachment 203600 [details]
Proposed changes and test
Comment 3 Denise Smith CLA 2011-09-19 15:15:38 EDT
Created attachment 203623 [details]
Additional changes and tests
Comment 4 Denise Smith CLA 2011-09-20 10:42:50 EDT
Fixed in MappingsGenerator to track generated prefix/uri pairs in the globalnamespaceresolver and re use when possible.

Also modified XMLOBjectBuilder to update the value of the type indicator field if possible to use the prefix from the row.
Comment 5 Eclipse Webmaster CLA 2022-06-09 10:02:33 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink