Repository: cxf Updated Branches: refs/heads/master-jaxrs-2.1 e0448d1fd -> 81974b125 (forced update)
[CXF-6992] Passing correct class and generic type Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/7ca70dcb Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/7ca70dcb Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/7ca70dcb Branch: refs/heads/master-jaxrs-2.1 Commit: 7ca70dcb78240d2f1d312c1255480207609abd84 Parents: 3599f93 Author: Sergey Beryozkin <[email protected]> Authored: Thu Aug 25 12:45:11 2016 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Thu Aug 25 12:45:11 2016 +0100 ---------------------------------------------------------------------- .../apache/cxf/jaxrs/utils/InjectionUtils.java | 20 +++++++-- .../java/org/apache/cxf/jaxrs/Customer.java | 14 ++++++- .../apache/cxf/jaxrs/utils/JAXRSUtilsTest.java | 44 ++++++++++++++++++-- 3 files changed, 69 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/7ca70dcb/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java index 2414476..8688a18 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java @@ -889,9 +889,21 @@ public final class InjectionUtils { //CHECKSTYLE:ON Class<?> type = getCollectionType(rawType); - Class<?> realType = rawType.isArray() ? rawType.getComponentType() - : InjectionUtils.getActualType(genericType); - + Class<?> realType = null; + Type realGenericType = null; + if (rawType.isArray()) { + realType = rawType.getComponentType(); + realGenericType = realType; + } else { + Type[] types = getActualTypes(genericType); + if (types.length == 0 || !(types[0] instanceof ParameterizedType)) { + realType = getActualType(genericType); + realGenericType = realType; + } else { + realType = getRawType(types[0]); + realGenericType = types[0] == realType ? realType : types[0]; + } + } Object theValues = null; if (type != null) { try { @@ -912,7 +924,7 @@ public final class InjectionUtils { valuesList = checkPathSegment(valuesList, realType, pathParam); for (int ind = 0; ind < valuesList.size(); ind++) { Object o = InjectionUtils.handleParameter(valuesList.get(ind), decoded, - realType, realType, paramAnns, pathParam, message); + realType, realGenericType, paramAnns, pathParam, message); addToCollectionValues(theValues, o, ind); } } http://git-wip-us.apache.org/repos/asf/cxf/blob/7ca70dcb/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java index ef01347..f55fc1d 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java @@ -379,10 +379,20 @@ public class Customer extends AbstractCustomer implements CustomerInfo { @QueryParam("query3") List<Integer> queryString3, @QueryParam("query3") Integer[] queryString3Array, @QueryParam("query4") List<String> queryString4, - @QueryParam("query5") List<String> queryString5) { + @QueryParam("query5") List<String> queryString5, + @QueryParam("query3") List<MyType<Integer>> queryString6) { // complete } - + public static class MyType<T> { + private T t; + public MyType(T t) { + this.t = t; + } + public T get() { + return t; + } + + } @Produces("text/xml") public void testPathSegment(@PathParam("ps") PathSegment ps, @PathParam("ps") String path) { http://git-wip-us.apache.org/repos/asf/cxf/blob/7ca70dcb/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java index 4d747bb..04d5455 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java @@ -23,6 +23,7 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -61,6 +62,7 @@ import org.apache.cxf.endpoint.Server; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.jaxrs.Customer; import org.apache.cxf.jaxrs.Customer.CustomerContext; +import org.apache.cxf.jaxrs.Customer.MyType; import org.apache.cxf.jaxrs.Customer.Query; import org.apache.cxf.jaxrs.Customer2; import org.apache.cxf.jaxrs.CustomerApplication; @@ -894,16 +896,18 @@ public class JAXRSUtilsTest extends Assert { @Test public void testQueryParamAsListWithDefaultValue() throws Exception { Class<?>[] argType = {List.class, List.class, List.class, Integer[].class, - List.class, List.class}; + List.class, List.class, List.class}; Method m = Customer.class.getMethod("testQueryAsList", argType); Message messageImpl = createMessage(); + ProviderFactory.getInstance(messageImpl) + .registerUserProvider(new MyTypeParamConverterProvider()); messageImpl.put(Message.QUERY_STRING, "query2=query2Value&query2=query2Value2&query3=1&query3=2&query4"); List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl); - assertEquals(6, params.size()); + assertEquals(7, params.size()); List<String> queryList = (List<String>)params.get(0); assertNotNull(queryList); assertEquals(1, queryList.size()); @@ -935,6 +939,12 @@ public class JAXRSUtilsTest extends Assert { List<String> queryList5 = (List<String>)params.get(5); assertNotNull(queryList5); assertEquals(0, queryList5.size()); + + List<MyType<Integer>> queryList6 = (List<MyType<Integer>>)params.get(6); + assertNotNull(queryList6); + assertEquals(2, queryList6.size()); + assertEquals(Integer.valueOf(1), queryList6.get(0).get()); + assertEquals(Integer.valueOf(2), queryList6.get(1).get()); } @Test @@ -2076,7 +2086,35 @@ public class JAXRSUtilsTest extends Assert { e.put(Endpoint.class, endpoint); return m; } - + static class MyTypeParamConverterProvider + implements ParamConverterProvider, ParamConverter<MyType<Integer>> { + + @Override + public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, + Annotation[] annotations) { + if (rawType == MyType.class) { + Type type = ((ParameterizedType)genericType).getActualTypeArguments()[0]; + @SuppressWarnings("unchecked") + ParamConverter<T> converter = (ParamConverter<T>)this; + if (type == Integer.class) { + return converter; + } + } + return null; + } + + @Override + public MyType<Integer> fromString(String value) { + return new MyType<Integer>(Integer.valueOf(value)); + } + + @Override + public String toString(MyType<Integer> value) { + // TODO Auto-generated method stub + return null; + } + + } private static class LocaleParameterHandler implements ParamConverterProvider, ParamConverter<Locale> { @SuppressWarnings("unchecked")
