Pages

Monday, November 22, 2010

Jibx issue with primitives when made "Optional"

Last week, I have been struggling with  a issue in Jibx.It was a very simple change as a code fix, it took me one whole day. We have to make certain fields optional in a request object. This object will be converted in to an XML using Jibx and then will be submitted to an external web service. We put the one of the boolean value as optional.Since we are defaulting the value in the object to false, this change should not affect anything big.Because of this confidence, we started testing with the client. But it errors out saying required filed is missing . Ok  we made it optional(here it will be never optional as it will always have value either true or false), but since it has a default value always it should not act as optional, whlie submitting the request.
 Jibx Mapping:
<mapping class="com.sample.model.Account"
             name="client-account"
             flexible="true"
             ordered="false">
        <value name="test-account"
               style="element"
               get-method="isTestAccount"
               set-method="setTestAccount"
               usage="optional"/>
        <value name="status"
               style="element"
               get-method="getStatus"
               set-method="setStatus"
               usage="optional"  />
        <value name="payment-type"
               style="element"
               get-method="getPaymentType"
               set-method="setPaymentType"
               usage="optional"  />
        <value name="notes"
               style="element"
               get-method="getNotes"
               set-method="setNotes"
               usage="optional"  />
    </mapping>

Marshalling Result :

test-account  = false
<?xml version="1.0" encoding="UTF-8"?><client-account><status>Good</status><payment-type>Pre-Pay</payment-type><notes>unlimited</notes></client-account>

test-account  = true
<?xml version="1.0" encoding="UTF-8"?><client-account><test-account>true</test-account><status>Good</status><payment-type>Pre-Pay</payment-type><notes>unlimited</notes></client-account>

Here it is very clear that Jibx is not putting the elements if the value is false. It took hours to arrive at this conclusion due to the complexity of the testing procedure.

Finally we found the answer from Dennis Sosnoski saying :(here)
When you set optional='true' on a primitive value, JiBX checks if the value is equal to the default and if so does not include the value when marshalling. If you'd like to have effectively a three-state representation - yes/no/not present - you can do so by using a Boolean value in place of the boolean. Alternatively, you can use a test-method to check if the value is present when marshalling.

It is worth knowing especially if you committed  a change like this  as "simple and easy " to your manager :-)

Update: We had another occurrence of this problem with a primitive integer. The field  was optional so when the value is "0"(zero) jibx omits that element in the marshaled xml.Earlier we were thinking only about the 'boolean' types.





No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...