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

Bug 330707

Summary: Dynamic MOXy should support JAXB binding file
Product: z_Archived Reporter: Mike Norman <michael.norman>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED DUPLICATE QA Contact:
Severity: normal    
Priority: P3    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Mike Norman CLA 2010-11-19 14:57:08 EST
Setup a (relatively complex) schema that imports an other schema:
when the instance doc is unmarshalled, the DynamicEntity fields
are wrapped in JAXBElement;s:

    static final String SRVC_SCHEMA =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
        "<xsd:schema " +
         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
         "targetNamespace=\"urn:simpleService\" " +
         "xmlns=\"urn:simpleService\" " +
         "xmlns:srvc=\"urn:simpleService\" " +
         "xmlns:ns1=\"urn:simpletable\" " +
         "elementFormDefault=\"qualified\">" +
           "<xsd:import namespace=\"urn:simpletable\" schemaLocation=\"simple.xsd\"/>" +
           "<xsd:complexType name=\"findByPrimaryKey_simpletableTypeResponseType\">" +
              "<xsd:sequence>" +
                 "<xsd:element name=\"result\">" +
                    "<xsd:complexType>" +
                       "<xsd:sequence>" +
                          "<xsd:element minOccurs=\"0\" ref=\"ns1:simpletableType\"/>" +
                       "</xsd:sequence>" +
                    "</xsd:complexType>" +
                 "</xsd:element>" +
              "</xsd:sequence>" +
           "</xsd:complexType>" +
           "<xsd:complexType name=\"findByPrimaryKey_simpletableTypeRequestType\">" +
             "<xsd:sequence>" +
               "<xsd:element name=\"id\" type=\"xsd:decimal\"/>" +
             "</xsd:sequence>" +
           "</xsd:complexType>" +
           "<xsd:element name=\"findByPrimaryKey_simpletableTypeResponse\" type=\"srvc:findByPrimaryKey_simpletableTypeResponseType\"/>" +
           "<xsd:element name=\"findByPrimaryKey_simpletableType\" type=\"srvc:findByPrimaryKey_simpletableTypeRequestType\"/>" +
        "</xsd:schema>";
    static final String SIMPLE_SCHEMA =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
        "<xsd:schema " +
         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
         "targetNamespace=\"urn:simpletable\" " +
         "xmlns=\"urn:simpletable\" " +
         "elementFormDefault=\"qualified\">" +
           "<xsd:complexType name=\"simpletableType\">" +
           "<xsd:sequence>" +
               "<xsd:element name=\"id\" type=\"xsd:decimal\"/>" +
               "<xsd:element name=\"name\" type=\"xsd:string\" minOccurs=\"0\" nillable=\"true\"/>" +
               "<xsd:element name=\"since\" type=\"xsd:date\" minOccurs=\"0\" nillable=\"true\"/>" +
           "</xsd:sequence>" +
           "</xsd:complexType>" +
           "<xsd:element name=\"simpletableType\" type=\"simpletableType\"/>" +
        "</xsd:schema>";
    @Test
    public void testDynamicJAXB() throws FileNotFoundException, JAXBException {
        DynamicJAXBContext dynamicJAXBContext = 
            DynamicJAXBContextFactory.createContextFromXSD(new StreamSource(
                new StringReader(SRVC_SCHEMA)),
                new MyEntityResolver2(), null, null);
        JAXBUnmarshaller unmarshaller = dynamicJAXBContext.createUnmarshaller();
        Object o = unmarshaller.unmarshal(new StringReader(RESPONSE));
        JAXBElement<DynamicEntity> responseEl = (JAXBElement<DynamicEntity>)o;
        DynamicEntity response = responseEl.getValue();
        DynamicEntity result = response.get("result");
        DynamicEntity simpleEntity = result.get("simpletableType");
        System.out.println();
        System.out.println(simpleEntity.get("id"));
        System.out.println(simpleEntity.get("name"));
        System.out.println(simpleEntity.get("since"));
    }
    static final String RESPONSE = 
        "<ns1:findByPrimaryKey_simpletableTypeResponse xmlns:ns1=\"urn:simpleService\" xmlns=\"urn:simpletable\">" +
          "<ns1:result>" +
            "<simpletableType>" +
              "<id>1</id>" +
              "<name>mike</name>" +
              "<since>2001-12-25</since>" +
            "</simpletableType>" +
          "</ns1:result>" +
        "</ns1:findByPrimaryKey_simpletableTypeResponse>";
Comment 1 Mike Norman CLA 2010-11-19 14:57:37 EST
    private class MyEntityResolver2 implements EntityResolver {
        //assume inline schemas are in same directory as wsdl
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
           InputSource inputSource = new InputSource(new StringReader(SIMPLE_SCHEMA));
           inputSource.setSystemId("file:/simple.xsd");
           return inputSource;
        }
      
     }
Comment 2 Mike Norman CLA 2010-11-26 10:23:31 EST
It turns out that the behaviour described above is expected because
of the nillable="true" attribute for "name" and "since". The JAXBElement
tracks the 'isSet' status of the field in addition to regular values and NULL.

A client could customize this behaviour with a JAXB binding file - so this
bug is taking a bit of a left turn and will instead track adding the
binding file to the API.
Comment 3 Mike Norman CLA 2011-01-28 13:49:14 EST

*** This bug has been marked as a duplicate of bug 331210 ***
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:20:18 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink