CXF
  1. CXF
  2. CXF-2390

Enums using fromString() but jaxb creates fromValue()

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.2.2
    • Fix Version/s: 2.2.4, 2.3
    • Component/s: JAX-RS
    • Labels:
      None
    • Environment:

      CXF version 2.2.2
      Jaxb version 2.1.9 ? Not positive on this

      Description

      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?

        Activity

          People

          • Assignee:
            Sergey Beryozkin
            Reporter:
            David Wood
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development