Friday, September 21, 2012

Undefined elements in structs returned from web service

I was accessing some functions remotely today in CF10 when I came across some weird behavior. Namely, the values returned by StructkeyList, StructkeyExists, and a Dump of a Struct are consistent with each other.

Take the following code and place it somewhere in a CFC to be accessed remotely.

    <cffunction name="getTest" output="no" returntype="struct" access="remote">

        <cfset result=structnew()>
                <cfset result.testvar = "some var">
                <cfset result.fuseactions = ArrayNew(1)>
   
        <cfreturn result>
   
    </cffunction>

Now access it from a calling page. You'll need to put in your own server and CFC name.

<cfinvoke
    webservice="http://xxx/mycfc.cfc?wsdl"
    returnvariable = "response"
    refreshWSDL = "true"
    method="gettest"
/>

This is where it gets interesting. Let's do a dump of our return variable.

Notice that the "FUSEACTIONS" key in our struct seems interesting. Here we see "undefined" where we would have expected an empty array. I'm guessing the empty array goes missing during the serialization.

Ok, but let's go further. Let's do #structKeyList(response)# on the struct. We see, as expected, our two elements.


Now it gets strange. Try to do a StructKeyExists on the FUSEACTIONS key.

#structkeyExists(response,'FUSEACTIONS')#

It delivers "NO". Trying to dump the struct element results in an error.



Now I know that the concept of NULL values was introduced in CF9. (Something I missed, still toddling along with CF8). So let's try:

#isnull(response.FUSEACTIONS)#

Sure enough, it delivers YES.

Still, this seems a little weird to me. Intuitively I would expect to see an empty array inside the Struct, since that was how the variable was declared. In any case, the inconsistency between StructKeyExists and StructKeyList is a change from CF8 and it broke some old code of ours.

 I've filed this as bug 3334756 by Adobe. Let's see what they say.



No comments:

Post a Comment