From 1b21e867bff754e03a97cacde434206c7d87c178 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Fri, 11 Mar 2022 06:35:38 +0200 Subject: [PATCH] Support wildcards in generic return types of JAX-RS methods Fixes: #24250 (cherry picked from commit a9816fa0c48c5093241df0f90f19ce4ea4bc7f9a) --- .../core/startup/RuntimeResourceDeployment.java | 16 ++++++++++++---- .../test/response/RestResponseResource.java | 6 ++++++ .../vertx/test/response/RestResponseTest.java | 4 ++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeResourceDeployment.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeResourceDeployment.java index 65f48b79e56da..2b75729980b3f 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeResourceDeployment.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeResourceDeployment.java @@ -8,6 +8,7 @@ import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.lang.reflect.WildcardType; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; @@ -630,20 +631,27 @@ private Type getEffectiveReturnType(Type returnType) { return returnType; if (returnType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) returnType; + Type firstTypeArgument = type.getActualTypeArguments()[0]; if (type.getRawType() == CompletionStage.class) { - return getEffectiveReturnType(type.getActualTypeArguments()[0]); + return getEffectiveReturnType(firstTypeArgument); } if (type.getRawType() == Uni.class) { - return getEffectiveReturnType(type.getActualTypeArguments()[0]); + return getEffectiveReturnType(firstTypeArgument); } if (type.getRawType() == Multi.class) { - return getEffectiveReturnType(type.getActualTypeArguments()[0]); + return getEffectiveReturnType(firstTypeArgument); } if (type.getRawType() == RestResponse.class) { - return getEffectiveReturnType(type.getActualTypeArguments()[0]); + return getEffectiveReturnType(firstTypeArgument); } return returnType; } + if (returnType instanceof WildcardType) { + Type[] bounds = ((WildcardType) returnType).getLowerBounds(); + if (bounds.length > 0) + return getRawType(bounds[0]); + return getRawType(((WildcardType) returnType).getUpperBounds()[0]); + } throw new UnsupportedOperationException("Endpoint return type not supported yet: " + returnType); } diff --git a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/response/RestResponseResource.java b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/response/RestResponseResource.java index b246794fbcb82..286b0e1be476c 100644 --- a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/response/RestResponseResource.java +++ b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/response/RestResponseResource.java @@ -26,6 +26,12 @@ public RestResponse getString() { return RestResponse.ok("Hello"); } + @GET + @Path("rest-response-wildcard") + public RestResponse wildcard() { + return RestResponse.ResponseBuilder.ok("Hello").header("content-type", "text/plain").build(); + } + @GET @Path("rest-response-full") public RestResponse getResponse() throws URISyntaxException { diff --git a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/response/RestResponseTest.java b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/response/RestResponseTest.java index 6113359c1077e..01196ef4b16af 100644 --- a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/response/RestResponseTest.java +++ b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/response/RestResponseTest.java @@ -27,6 +27,10 @@ public void test() { .then().statusCode(200) .and().body(Matchers.equalTo("Hello")) .and().contentType("text/plain"); + RestAssured.get("/rest-response-wildcard") + .then().statusCode(200) + .and().body(Matchers.equalTo("Hello")) + .and().contentType("text/plain"); RestAssured.get("/rest-response-full") .then() .statusCode(200)