Gurus,
I have been playing with the default value plugin and I notice some flaws.
Lets assume a data object FOO has the following field defined in the WSDL:
<xs:element default="42" minOccurs="0" name="defaultIntegerValue"
type="xs:int"/>
The code generated using the CXF's wsdl2java argument -xjc-Xdv looks like this:
@XmlElement(defaultValue = "42")
protected Integer defaultIntegerValue;
public Integer getDefaultIntegerValue() {
if (null == defaultIntegerValue) {
return new Integer(42);
}
return defaultIntegerValue;
}
Here are the issues I see:
1) This assumes that null is not a valid value for
defaultIntegerValue. Just because something has a default value does
not mean that null is not a valid value. Being null and having a
default value are two orthogonal properties.
To specifically state the issue, if someone explicitly set the field
defaultIntegerValue to null, then this code would ignore that setting.
If there is a standard that motivated this behavior please let me
know.
2) Further, the default accesstype of generated client side code is
javax.xml.bind.annotation.XmlAccessType.FIELD. So, any code generated
by the Default Value plugin is ignored - since the getters are never
even called! This second issue makes the first one irrelevant.
As it stands today, this seems buggy to me. I believe it would be more
accurate for the default value plugin to just initialize the fields
and not mess with the getters at all. This would solve both the above
stated issues. Here is what I am thinking should be generated:
@XmlElement(defaultValue = "42")
protected Integer defaultIntegerValue = new Integer(42);
//Do not mess with the getters
This is inline with JAXBs marshalling suggestion here:
https://jaxb.java.net/guide/Element_default_values_and_marshalling.html
Cheers
Rouble