Skip to content

Commit

Permalink
Instantiate collections for DynamicType properties (#2256)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtatt authored Sep 9, 2024
1 parent c4fc6f1 commit 1b44275
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.persistence.mappings.OneToOneMapping;
import org.eclipse.persistence.mappings.converters.EnumTypeConverter;
import org.eclipse.persistence.mappings.foundation.AbstractDirectMapping;
import org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping;
import org.eclipse.persistence.platform.database.DatabasePlatform;
import org.eclipse.persistence.platform.xml.XMLParser;
import org.eclipse.persistence.platform.xml.XMLPlatformFactory;
Expand Down Expand Up @@ -214,6 +215,9 @@ private boolean requiresInitialization(DatabaseMapping mapping) {
if (mapping.isDirectToFieldMapping() && mapping.getAttributeClassification() != null && mapping.getAttributeClassification().isPrimitive()) {
return true;
}
if (mapping.isCollectionMapping()) {
return true;
}
if (mapping.isForeignReferenceMapping()) {
ForeignReferenceMapping frMapping = (ForeignReferenceMapping) mapping;
return frMapping.usesIndirection() || frMapping.isCollectionMapping();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -78,6 +78,9 @@ else if (primClass == ClassConstants.PBYTE) {
value = Byte.MIN_VALUE;
}
}
else if (mapping.isCollectionMapping()) {
value = mapping.getContainerPolicy().containerInstance();
}
else if (mapping.isForeignReferenceMapping()) {
ForeignReferenceMapping refMapping = (ForeignReferenceMapping)mapping;
if (refMapping.usesIndirection() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.ArrayList;

import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.Marshaller;
Expand All @@ -33,6 +34,7 @@
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
import org.eclipse.persistence.oxm.NamespaceResolver;
import org.eclipse.persistence.testing.jaxb.dynamic.util.MyList;
import org.w3c.dom.Document;

Expand Down Expand Up @@ -65,6 +67,23 @@ public void testXSDSingleListUnmarshal() throws Exception {
assertEquals("Incorrect phoneNumber type.", "work", firstPhoneNumber.get("type"));
}

public void testXSDInstantiateList() throws Exception {
InputStream schemaStream = ClassLoader.getSystemResourceAsStream(XSD_SINGLE);
jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(schemaStream, null, null, null);

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

assertNotNull(customer.get("phoneNumbers"));

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

var phoneNumbers = jaxbContext.getValueByXPath(customer, "phone-numbers", resolver, Object.class);

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

public void testXSDMultipleListUnmarshal() throws Exception {
InputStream schemaStream = ClassLoader.getSystemResourceAsStream(XSD_MULTI);
jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(schemaStream, null, null, null);
Expand Down

0 comments on commit 1b44275

Please sign in to comment.