Fix for bug 389092, reviewed by bdoughan
diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/mappings/XMLDirectMapping.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/mappings/XMLDirectMapping.java
index 6a8d2e9..00c3186 100644
--- a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/mappings/XMLDirectMapping.java
+++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/mappings/XMLDirectMapping.java
@@ -190,6 +190,7 @@
private boolean isWriteOnly;
private boolean isCollapsingStringValues;
private boolean isNormalizingStringValues;
+ private boolean isNullValueMarshalled = false;
public XMLDirectMapping() {
super();
@@ -312,7 +313,8 @@
// PERF: This method is a major performance code point,
// so has been micro optimized and uses direct variable access.
Object fieldValue = attributeValue;
- if ((this.nullValue != null) && (this.nullValue.equals(fieldValue)) && !((XMLField)field).isRequired()) {
+
+ if ((this.nullValue != null) && (this.nullValue.equals(fieldValue)) && !this.isNullValueMarshalled && !((XMLField)field).isRequired()) {
return null;
}
@@ -463,7 +465,7 @@
* Indicates that this mapping should collapse all string values before setting them
* in the object on unmarshal. Collapse removes leading and trailing whitespaces, and replaces
* any sequence of whitepsace characters with a single space.
- * @param normalize
+ * @param collapse
*/
public void setCollapsingStringValues(boolean collapse) {
this.isCollapsingStringValues = collapse;
@@ -474,10 +476,27 @@
* Returns true if this mapping should collapse all string values before setting them
* in the object on unmarshal. Collapse removes leading and trailing whitespaces, and replaces
* any sequence of whitepsace characters with a single space.
- * @param normalize
*/
public boolean isCollapsingStringValues() {
return this.isCollapsingStringValues;
}
-
+
+ /**
+ * PUBLIC:
+ * Returns true if this mapping's value should be marshalled, in the case that
+ * it is equal to the default null value.
+ */
+ public boolean isNullValueMarshalled() {
+ return this.isNullValueMarshalled;
+ }
+
+ /**
+ * PUBLIC:
+ * Set whether this mapping's value should be marshalled, in the case that
+ * it is equal to the default null value.
+ */
+ public void setNullValueMarshalled(boolean value) {
+ this.isNullValueMarshalled = value;
+ }
+
}
diff --git a/moxy/eclipselink.moxy.test/resource/org/eclipse/persistence/testing/jaxb/xmlelement/employee_defaultvalue.json b/moxy/eclipselink.moxy.test/resource/org/eclipse/persistence/testing/jaxb/xmlelement/employee_defaultvalue.json
new file mode 100644
index 0000000..49a0024
--- /dev/null
+++ b/moxy/eclipselink.moxy.test/resource/org/eclipse/persistence/testing/jaxb/xmlelement/employee_defaultvalue.json
@@ -0,0 +1,4 @@
+{"employee":{
+ "name":"DEFAULT_NAME"
+ }
+}
\ No newline at end of file
diff --git a/moxy/eclipselink.moxy.test/resource/org/eclipse/persistence/testing/jaxb/xmlelement/employee_defaultvalue.xml b/moxy/eclipselink.moxy.test/resource/org/eclipse/persistence/testing/jaxb/xmlelement/employee_defaultvalue.xml
new file mode 100644
index 0000000..5d17f08
--- /dev/null
+++ b/moxy/eclipselink.moxy.test/resource/org/eclipse/persistence/testing/jaxb/xmlelement/employee_defaultvalue.xml
@@ -0,0 +1,4 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<employee>
+ <name>DEFAULT_NAME</name>
+</employee>
\ No newline at end of file
diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlelement/EmployeeDefaultValue.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlelement/EmployeeDefaultValue.java
new file mode 100644
index 0000000..8b6413a
--- /dev/null
+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlelement/EmployeeDefaultValue.java
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * Copyright (c) 1998, 2012 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 v1.0 and Eclipse Distribution License v. 1.0
+ * which accompanies this distribution.
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ * rbarkhouse - 2.4 - initial implementation
+ ******************************************************************************/
+package org.eclipse.persistence.testing.jaxb.xmlelement;
+
+import javax.xml.bind.annotation.*;
+
+@XmlRootElement(name = "employee")
+public class EmployeeDefaultValue {
+
+ public static final String DEFAULT_NAME = "DEFAULT_NAME";
+
+ @XmlElement(defaultValue = DEFAULT_NAME)
+ public String name;
+
+ public String toString() {
+ return "EMPLOYEE: " + this.name;
+ }
+
+ public boolean equals(Object object) {
+ EmployeeDefaultValue emp = ((EmployeeDefaultValue) object);
+ return emp.name.equals(this.name);
+ }
+
+}
\ No newline at end of file
diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlelement/XmlElementDefaultValueTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlelement/XmlElementDefaultValueTestCases.java
new file mode 100644
index 0000000..e6c6205
--- /dev/null
+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlelement/XmlElementDefaultValueTestCases.java
@@ -0,0 +1,39 @@
+/*******************************************************************************
+ * Copyright (c) 1998, 2012 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 v1.0 and Eclipse Distribution License v. 1.0
+ * which accompanies this distribution.
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ * rbarkhouse - 2.4 - initial implementation
+ ******************************************************************************/
+package org.eclipse.persistence.testing.jaxb.xmlelement;
+
+import java.util.ArrayList;
+
+import org.eclipse.persistence.testing.jaxb.JAXBWithJSONTestCases;
+
+public class XmlElementDefaultValueTestCases extends JAXBWithJSONTestCases {
+
+ private final static String XML_RESOURCE = "org/eclipse/persistence/testing/jaxb/xmlelement/employee_defaultvalue.xml";
+ private final static String JSON_RESOURCE = "org/eclipse/persistence/testing/jaxb/xmlelement/employee_defaultvalue.json";
+
+ public XmlElementDefaultValueTestCases(String name) throws Exception {
+ super(name);
+ setControlDocument(XML_RESOURCE);
+ setControlJSON(JSON_RESOURCE);
+ Class[] classes = new Class[1];
+ classes[0] = EmployeeDefaultValue.class;
+ setClasses(classes);
+ }
+
+ protected Object getControlObject() {
+ EmployeeDefaultValue employee = new EmployeeDefaultValue();
+ employee.name = EmployeeDefaultValue.DEFAULT_NAME;
+ return employee;
+ }
+
+}
\ No newline at end of file
diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlelement/XmlElementTestSuite.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlelement/XmlElementTestSuite.java
index c2f8ab3..5a22ebe 100644
--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlelement/XmlElementTestSuite.java
+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlelement/XmlElementTestSuite.java
@@ -39,6 +39,7 @@
suite.addTestSuite(SpecialCharacterTestCases.class);
suite.addTestSuite(ElementOrderingTestCases.class);
suite.addTestSuite(XmlElementConstantsTestCases.class);
+ suite.addTestSuite(XmlElementDefaultValueTestCases.class);
return suite;
}
diff --git a/moxy/org.eclipse.persistence.moxy/src/org/eclipse/persistence/jaxb/compiler/MappingsGenerator.java b/moxy/org.eclipse.persistence.moxy/src/org/eclipse/persistence/jaxb/compiler/MappingsGenerator.java
index 5ebad41..c9d56d8 100644
--- a/moxy/org.eclipse.persistence.moxy/src/org/eclipse/persistence/jaxb/compiler/MappingsGenerator.java
+++ b/moxy/org.eclipse.persistence.moxy/src/org/eclipse/persistence/jaxb/compiler/MappingsGenerator.java
@@ -390,6 +390,7 @@
xmlDescriptor.addMapping(mapping);
} else {
XMLDirectMapping mapping = new XMLDirectMapping();
+ mapping.setNullValueMarshalled(true);
mapping.setAttributeName("value");
mapping.setGetMethodName("getValue");
mapping.setSetMethodName("setValue");
@@ -403,6 +404,7 @@
XMLDirectMapping mapping = new XMLDirectMapping();
mapping.setConverter(buildJAXBEnumTypeConverter(mapping, enumInfo));
+ mapping.setNullValueMarshalled(true);
mapping.setAttributeName("value");
mapping.setGetMethodName("getValue");
mapping.setSetMethodName("setValue");
@@ -1282,6 +1284,7 @@
public XMLDirectMapping generateDirectMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo) {
XMLDirectMapping mapping = new XMLDirectMapping();
+ mapping.setNullValueMarshalled(true);
mapping.setAttributeName(property.getPropertyName());
String fixedValue = property.getFixedValue();
if (fixedValue != null) {
@@ -1514,6 +1517,7 @@
public XMLDirectMapping generateDirectEnumerationMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo, EnumTypeInfo enumInfo) {
XMLDirectMapping mapping = new XMLDirectMapping();
+ mapping.setNullValueMarshalled(true);
mapping.setConverter(buildJAXBEnumTypeConverter(mapping, enumInfo));
mapping.setAttributeName(property.getPropertyName());
if (property.isMethodProperty()) {
@@ -1865,6 +1869,7 @@
} else {
mapping = new XMLDirectMapping();
mapping.setAttributeName(attributeName);
+ ((XMLDirectMapping)mapping).setNullValueMarshalled(true);
((XMLDirectMapping)mapping).setXPath(attributeName + TXT);
QName schemaType = (QName) userDefinedSchemaTypes.get(theType.getQualifiedName());
@@ -2944,6 +2949,7 @@
}else{
XMLDirectMapping mapping = new XMLDirectMapping();
+ mapping.setNullValueMarshalled(true);
mapping.setAttributeName("value");
mapping.setXPath("text()");
mapping.setSetMethodName("setValue");