You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cxf.apache.org by "Sergey Beryozkin (JIRA)" <ji...@apache.org> on 2009/08/12 15:23:14 UTC

[jira] Resolved: (CXF-2390) Enums using fromString() but jaxb creates fromValue()

     [ https://issues.apache.org/jira/browse/CXF-2390?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Sergey Beryozkin resolved CXF-2390.
-----------------------------------

       Resolution: Fixed
    Fix Version/s: 2.3
                   2.2.4
         Assignee: Sergey Beryozkin

Will merge to 2.2.4-SNAPSHOT a bit later


> Enums using fromString() but jaxb creates fromValue()
> -----------------------------------------------------
>
>                 Key: CXF-2390
>                 URL: https://issues.apache.org/jira/browse/CXF-2390
>             Project: CXF
>          Issue Type: Bug
>          Components: REST
>    Affects Versions: 2.2.2
>         Environment: CXF version 2.2.2
> Jaxb version 2.1.9 ? Not positive on this
>            Reporter: David Wood
>            Assignee: Sergey Beryozkin
>             Fix For: 2.2.4, 2.3
>
>
> Let's say you have an xsd file that defines an enum.
>   <xs:simpleType name="timezone">
>     <xs:restriction base="xs:string">
>       <xs:enumeration value="Europe/London" />
>       <xs:enumeration value="America/New_York" />
>     </xs:restriction>
>   </xs:simpleType>
> When xjc (jaxb) runs and creates a Java enum for this, it gives the Enum instances slightly different names, but makes the underlying value available.
> @XmlType(name = "timezone")
> @XmlEnum
> public enum Timezone {
>     @XmlEnumValue("Europe/London")
>     EUROPE_LONDON("Europe/London"),
>     @XmlEnumValue("America/New_York")
>     AMERICA_NEW_YORK("America/New_York"),
> It also creates a "fromValue()" method to retrieve an enum instance given the value.
> Now say that you want to create a REST web service that has a Timezone as a QueryParam
> @GET
> @Path("/test")
> public Result theTest(@QueryParam("timezone") Timezone timezone)
> If you attempt to call this REST method with a timezone of "Europe%2FLondon", you'll get an exception 
> javax.ws.rs.WebApplicationException: java.lang.IllegalArgumentException: No enum const class beans.Timezone.Europe/London
>         at org.apache.cxf.jaxrs.utils.InjectionUtils.evaluateFactoryMethod(InjectionUtils.java:332)
>         at org.apache.cxf.jaxrs.utils.InjectionUtils.handleParameter(InjectionUtils.java:284)
>         at org.apache.cxf.jaxrs.utils.InjectionUtils.createParameterObject(InjectionUtils.java:649)
>         at org.apache.cxf.jaxrs.utils.JAXRSUtils.readQueryString(JAXRSUtils.java:763)
> The InjectionUtils class has this logic:
> // check for valueOf(String) static methods
>         String[] methodNames = pClass.isEnum() 
>             ? new String[] {"fromString", "valueOf"} 
>             : new String[] {"valueOf", "fromString"};
>         Object result = evaluateFactoryMethod(value, pClass, pType, methodNames[0]);
>         if (result == null) {
>             result = evaluateFactoryMethod(value, pClass, pType, methodNames[1]);
>         }
> Note that in the case of an Enum, it is trying to call "fromString()" (which doesn't exist), and failing that, it tries to call "valueOf()", which would work if the client had passed EUROPE_LONDON as the value.
> Perhaps different versions of xjc/jaxb create fromValue and some create fromString().  I'm not sure.  But clearly some versions of jaxb create a method called fromValue().
> The net result is that the service doesn't actually work if you pass it the value as defined in the restriction.  Can injection utils be changed to include an attempt to use fromValue() as well?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.