jaxb
  1. jaxb
  2. JAXB-927

optionalProperty="primitive" does not work

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.2.6
    • Fix Version/s: 2.2.9
    • Component/s: xjc
    • Labels:
      None
    • Environment:

      Platform: All
      OS: All

      Description

      When an xsd defines an optional boolean field, if the optionalProperty attribute is set to "primitive", it should bind as it did in 1.0, according to the spec, but it doesn't.

      For example, given the following xsd:

      <xs:element name="usage-auth-rate-charge">
          <xs:complexType>
              <xs:sequence>
                  <xs:element name="service-id" type="xs:string"/>
                  <xs:element name="pricepoint_custom_fields_required" type="xs:boolean" minOccurs="0"/>
              </xs:sequence>
          </xs:complexType>
      </xs:element>
      

      and specifying optionalProperty="primitive" in the binding xml, the generated class looks like this:

      public class UsageAuthRateCharge {
      ........
      public Boolean isPricepointCustomFieldsRequired() {
          return pricepointCustomFieldsRequired;
      }
      

      this method returns Boolean, not boolean as per the spec.

      The problem is that although boxing will work, if the supplied xml doesn't contain a value for pricepoint_custom_fields_required, the class's Boolean field is null, instead of false. So a NullPointerException will be seen when doing something like this:

      methodWhichTakesPrimitiveBooleanArg(myUsageAuthRateChargeInstance.isPricepointCustomFieldsRequired());

      In the spec, it says the following:

      optionalProperty controls how a JAXB property with a primitive base type that represents an optional non-nillable element/attribute is bound. If the attribute has the value "wrapper", then the base type of the JAXB property is the wrapper class for the primitive type. A user can indicate that this optional property is not set by calling the setter with "null" value. If the attribute's value is "primitive", it binds as it did in JAXB 1.0. If the attribute's value is "isSet", it binds the optional property using the primitive base type and also the isSet/unset methods are generated for the optional property. The attribute's default value is "wrapper".

      If you bind the above xml with jaxb1.0, you get generated objects with a method getDisplay() which returns boolean (primitive).

      If you use jaxb 2.0 with optionalProperty set to "primitive" you should get exactly the same thing - that's what the spec says.

      In JAXB 1.0, if minOccurs="0", and the xsd contains no default value for the boolean, booleans are set to false by default.

      NB This is a duplicate of 926 which was raised against an older version of the code. This bug affects 2.2.6.

        Activity

        Hide
        archenroot added a comment - - edited

        I tried with 2.2.7 and 2.2.8 version and they are also affected.

        So the expected result should be something like this in jaxb binding generated class?
        public Boolean myBoolean() {
        if (myBoolean== null)

        { return false; }

        return myBoolean;
        }

        Or the isNull should be prevented inside of jaxb and in case of positive test (the attribute is not presented) it should default to false?

        This issue is not only case of boolean type, but String as well, if you try to getString() attribute and it is also optional and not presented, you will fail the same way if you will try to any operation because it is also null.

        Show
        archenroot added a comment - - edited I tried with 2.2.7 and 2.2.8 version and they are also affected. So the expected result should be something like this in jaxb binding generated class? public Boolean myBoolean() { if (myBoolean== null) { return false; } return myBoolean; } Or the isNull should be prevented inside of jaxb and in case of positive test (the attribute is not presented) it should default to false? This issue is not only case of boolean type, but String as well, if you try to getString() attribute and it is also optional and not presented, you will fail the same way if you will try to any operation because it is also null.
        Hide
        mdarwin added a comment - - edited

        Hi archenroot,

        Strictly speaking, to conform to the spec, the generated class should return boolean not Boolean (when the optionalProperty flag is set to primitive).

        However, if the returned type is Boolean, that would work for me. We can rely on auto-unboxing, but it must never return a null (obviously).

        It doesn't matter to me whether it's done inside jaxb or in the generated class. Just as long as the myBoolean method either returns boolean, or returns Boolean and guarantees that it will never be null.

        Show
        mdarwin added a comment - - edited Hi archenroot, Strictly speaking, to conform to the spec, the generated class should return boolean not Boolean (when the optionalProperty flag is set to primitive). However, if the returned type is Boolean, that would work for me. We can rely on auto-unboxing, but it must never return a null (obviously). It doesn't matter to me whether it's done inside jaxb or in the generated class. Just as long as the myBoolean method either returns boolean, or returns Boolean and guarantees that it will never be null.
        Hide
        Iaroslav Savytskyi added a comment -

        Fixed in trunk.

        Show
        Iaroslav Savytskyi added a comment - Fixed in trunk.

          People

          • Assignee:
            Martin Grebac
            Reporter:
            mdarwin
          • Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: