Tuesday, March 3, 2009

Flex vs. Web Services (pt.1) enums

Yes, the title is Flex VERSUS web services (SOAP at least), since we just wasted far too much time trying to figure out some of the quirks.

1) Define a web services including something with an enum (I'm using Java, you use whatever you want)
2) Deploy your webservice. Your WSDL will contain something similar to:
<xs:simpleType name="fooEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="GOOD"/>
<xs:enumeration value="BAD"/>
<xs:enumeration value="UGLY"/>
</xs:restriction>
</xs:simpleType>
3) Generate your actionscript classes from the WSDL. You'll end up with a fooEnum.as file containing
package generated.webservices
{
import mx.utils.ObjectProxy;
import flash.utils.ByteArray;
import mx.rpc.soap.types.*;
/**
* Wrapper class for a operation required type
*/

public class FooEnum
{
/**
* Constructor, initializes the type class
*/
public function FooEnum() {}

[Inspectable(category="Generated values", enumeration="GOOD,BAD,UGLY", type="String")]
public var fooEnum:String;public function toString():String
{
return fooEnum.toString();
}

}
}

4) Try to use the web service. Everything will appear to be fine, EXCEPT that the enum will ALWAYS be null.
If you trace the XML sent down from the server (even using Wireshark), it all looks good.
If you look at the action script classes, they look fine. Eventually you'll find http://bugs.adobe.com/jira/browse/SDK-14800 and some related bugs. Basically, it appears Flex doesn't handle the enums yet (as of SDK 3.x) and the easiest thing to do is change all your enums to Strings =(
Hope you didn't already have people using that web service...

No comments: