eclipse-ee4j / eclipselink

Eclipselink project
https://eclipse.dev/eclipselink/
Other
198 stars 168 forks source link

DynamicType with collection property not initializing as empty collection #2255

Open timtatt opened 3 weeks ago

timtatt commented 3 weeks ago

Describe the bug DynamicEntity created from a DynamicType do not initialise collection properties with an empty list. Because the collections are not initialised, all XPath actions on these properties will result in NullPointerExceptions.

To Reproduce Steps/resources to reproduce the behavior:

Below is a simple test case to replicate the issue.

InputStream schemaStream = ClassLoader.getSystemResourceAsStream("single.xsd");
jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(schemaStream, null, null, null);

var customer = jaxbContext.newDynamicEntity("Customer");

assertNotNull(customer.get("phoneNumbers")); // currently this is null

var resolver = new NamespaceResolver();
resolver.setDefaultNamespaceURI("www.example.org/customer");

/*
currently throws a NullPointerException
java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "collection" is null

    at org.eclipse.persistence.core/org.eclipse.persistence.internal.oxm.Context.getValueByXPath(Context.java:551)
    at org.eclipse.persistence.core/org.eclipse.persistence.internal.oxm.Context.getValueByXPath(Context.java:528)
    at org.eclipse.persistence.core/org.eclipse.persistence.oxm.XMLContext.getValueByXPath(XMLContext.java:573)
    at org.eclipse.persistence.moxy/org.eclipse.persistence.jaxb.JAXBContext.getValueByXPath(JAXBContext.java:654)
*/
var phoneNumbers = jaxbContext.getValueByXPath(customer, "phone-numbers", resolver, Object.class);

assertNotNull(phoneNumbers);
assertEquals(ArrayList.class, phoneNumbers.getClass());

single.xsd

<xsd:schema
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns="www.example.org/customer"
   targetNamespace="www.example.org/customer"
   elementFormDefault="qualified">

   <xsd:complexType name="phone-number">
      <xsd:simpleContent>
         <xsd:extension base="xsd:string">
            <xsd:attribute name="type" type="xsd:string"/>
         </xsd:extension>
      </xsd:simpleContent>
   </xsd:complexType>

   <xsd:complexType name="customer">
      <xsd:sequence>
         <xsd:element name="phone-numbers" type="phone-number" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
   </xsd:complexType>

   <xsd:element name="customer" type="customer"/>

</xsd:schema>

Expected behavior Similar to how the static JAXB classes behave, new entities should be created with collection properties initialised with an empty collection.