cxf-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sergey Beryozkin (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CXF-5722) JAXB generated Enum throws IllegalArgumentException by unmarshalling as @QueryParam
Date Tue, 06 May 2014 11:04:15 GMT

    [ https://issues.apache.org/jira/browse/CXF-5722?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13990537#comment-13990537
] 

Sergey Beryozkin commented on CXF-5722:
---------------------------------------

Hi Jeroen

Thanks for the comments. 

I'd like to say that there's no guarantee that JAX_RS_SPEC-460 will be resolved.  While it
makes a perfect sense in this particular case, there could be a counter-argument coming in
from the spec experts that ignoring the exceptions thrown from fromValue() may cause the side-effects
because fromValue() may have been written to enforce the case sensitivity or some other requirements
as opposed to being a basic value matcher generated by JAXB.

I think it makes sense to relax the current spec text from the practical point of view, and
let a fromValue() to valueOf() transition happen if the former throws IllegalArgumentException,
but we can't guarantee it will be fixed as we expect.

We will keep the fix provided by Andrei either way but it may require setting an endpoint
property for it to be effective if JAX_RS_SPEC-460 gets refused.

Finally, note that JAX-RS 2.0 ParamConverterProvider can be used in CXF 3.0.0 onwards to 'fix'
this issue too

Cheers, Sergey


> JAXB generated Enum throws IllegalArgumentException by unmarshalling as @QueryParam
> -----------------------------------------------------------------------------------
>
>                 Key: CXF-5722
>                 URL: https://issues.apache.org/jira/browse/CXF-5722
>             Project: CXF
>          Issue Type: Bug
>          Components: JAX-RS
>    Affects Versions: 3.0.0-milestone2
>            Reporter: Andrei Shakirin
>            Assignee: Andrei Shakirin
>
> By processing request with Query parameter, InjectionUtils tries to recognize and instantiate
parameter class.
> In case of Enum parameter class, InjectionUtils.handleParameter() method calls in loop
following methods: "fromString", "fromValue", "valueOf" using reflection. If method returns
null, it tries the next one:
>         if (result == null) {
>             // check for valueOf(String) static methods
>             String[] methodNames = cls.isEnum() 
>                 ? new String[] {"fromString", "fromValue", "valueOf"} 
>                 : new String[] {"valueOf", "fromString"};
>             for (String mName : methodNames) {   
>                 result = evaluateFactoryMethod(value, cls, pType, mName);
>                 if (result != null) {
>                     break;
>                 }
>             }
> The problem is that factory methods in JAXB generated Enum behaves differently:
> - valueOf() accepts enum identifier (not value)
> - fromValue() accepts enum value
> Both methods throws IllegalArgumentException by worng argument and do not return null,
how current code expecting. Because formValue() is tried before valueOf(), it throws exception
and valueOf() is never tried, despite of fact that it will convert the value correctly.
> As a result following method throws IllegalArgumentException:
> 1) method:
>     @GET
>     @Path("/enum/")
>     Response checkEnum(@QueryParam("car") CarType car);
> 2) generated enum:
> @XmlRootElement(name = "Car")
> @XmlType(name = "carType")
> @XmlEnum
> public enum CarType {
>     @XmlEnumValue("Audi")
>     AUDI("Audi"),
>     @XmlEnumValue("Golf")
>     GOLF("Golf"),
>     BMW("BMW");
>     private final String value;
>     CarType(String v) {
>         value = v;
>     }
>     public String value() {
>         return value;
>     }
>     public static CarType fromValue(String v) {
>         for (CarType c: CarType.values()) {
>             if (c.value.equals(v)) {
>                 return c;
>             }
>         }
>         throw new IllegalArgumentException(v);
>     }
> }
> 3) call:
> customerService.checkEnum(CarType.AUDI);
> The issue was found by [Jeroen Vlek | https://issues.apache.org/jira/secure/ViewProfile.jspa?name=jeroenvlek].



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Mime
View raw message