Skip to content

Commit

Permalink
Support wildcards in generic return types of JAX-RS methods
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Mar 11, 2022
1 parent 6d5abf7 commit a9816fa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -631,20 +632,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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public RestResponse<String> 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<String> getResponse() throws URISyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a9816fa

Please sign in to comment.