Enums using fromString() but jaxb creates fromValue()
-----------------------------------------------------

                 Key: CXF-2390
                 URL: https://issues.apache.org/jira/browse/CXF-2390
             Project: CXF
          Issue Type: Bug
    Affects Versions: 2.2.2
         Environment: CXF version 2.2.2
Jaxb version 2.1.9 ? Not positive on this
            Reporter: David Wood


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.

Reply via email to