Skip to content

Commit

Permalink
Support CompletableFuture as a return type of RESTEasy Reactive Resou…
Browse files Browse the repository at this point in the history
…rce methods

Fixes: quarkusio#20207
(cherry picked from commit d376f1e)
  • Loading branch information
geoand committed Sep 28, 2021
1 parent e924b7d commit d8b0dbb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ public CompletionStage<String> asyncCompletionStageFail() {
return ret;
}

@GET
@Path("async/cf/ok")
public CompletableFuture<String> asyncCompletableFutureOK() {
return CompletableFuture.completedFuture("CF-OK");
}

@GET
@Path("async/cf/fail")
public CompletableFuture<String> asyncCompletableFutureFail() {
CompletableFuture<String> ret = new CompletableFuture<>();
ret.completeExceptionally(new TestException());
return ret;
}

@GET
@Path("async/uni/ok")
public Uni<String> asyncUniOK() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ public void testAsync() {
RestAssured.get("/simple/async/cs/fail")
.then().body(Matchers.equalTo("OK"))
.statusCode(666);
RestAssured.get("/simple/async/cf/ok")
.then().body(Matchers.equalTo("CF-OK"));
RestAssured.get("/simple/async/cf/fail")
.then().body(Matchers.equalTo("OK"))
.statusCode(666);
RestAssured.get("/simple/async/uni/ok")
.then().body(Matchers.equalTo("UNI-OK"));
RestAssured.get("/simple/async/uni/fail")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.BLOCKING;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.BOOLEAN;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.CHARACTER;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COMPLETABLE_FUTURE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COMPLETION_STAGE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.CONSUMES;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.CONTEXT;
Expand Down Expand Up @@ -685,6 +686,7 @@ private Type getNonAsyncReturnType(Type returnType) {
// NOTE: same code in RuntimeResourceDeployment.getNonAsyncReturnType
ParameterizedType parameterizedType = returnType.asParameterizedType();
if (COMPLETION_STAGE.equals(parameterizedType.name())
|| COMPLETABLE_FUTURE.equals(parameterizedType.name())
|| UNI.equals(parameterizedType.name())
|| MULTI.equals(parameterizedType.name())
|| REST_RESPONSE.equals(parameterizedType.name())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import javax.annotation.Priority;
import javax.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -84,6 +85,7 @@
import org.jboss.resteasy.reactive.RestQuery;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.RestSseElementType;
import org.reactivestreams.Publisher;

public final class ResteasyReactiveDotNames {

Expand Down Expand Up @@ -176,6 +178,8 @@ public final class ResteasyReactiveDotNames {
public static final DotName UNI = DotName.createSimple(Uni.class.getName());
public static final DotName MULTI = DotName.createSimple(Multi.class.getName());
public static final DotName COMPLETION_STAGE = DotName.createSimple(CompletionStage.class.getName());
public static final DotName COMPLETABLE_FUTURE = DotName.createSimple(CompletableFuture.class.getName());
public static final DotName PUBLISHER = DotName.createSimple(Publisher.class.getName());
public static final DotName REST_RESPONSE = DotName.createSimple(RestResponse.class.getName());

public static final DotName INTEGER = DotName.createSimple(Integer.class.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.jboss.resteasy.reactive.server.processor.scanning;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COMPLETABLE_FUTURE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COMPLETION_STAGE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.MULTI;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.PUBLISHER;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.UNI;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;
Expand All @@ -14,19 +17,14 @@
import org.jboss.resteasy.reactive.server.handlers.UniResponseHandler;
import org.jboss.resteasy.reactive.server.model.FixedHandlerChainCustomizer;
import org.jboss.resteasy.reactive.server.model.HandlerChainCustomizer;
import org.reactivestreams.Publisher;

public class AsyncReturnTypeScanner implements MethodScanner {
private static final DotName COMPLETION_STAGE = DotName.createSimple(CompletionStage.class.getName());
private static final DotName UNI = DotName.createSimple(Uni.class.getName());
private static final DotName MULTI = DotName.createSimple(Multi.class.getName());
private static final DotName PUBLISHER = DotName.createSimple(Publisher.class.getName());

@Override
public List<HandlerChainCustomizer> scan(MethodInfo method, ClassInfo actualEndpointClass,
Map<String, Object> methodContext) {
DotName returnTypeName = method.returnType().name();
if (returnTypeName.equals(COMPLETION_STAGE)) {
if (returnTypeName.equals(COMPLETION_STAGE) || returnTypeName.equals(COMPLETABLE_FUTURE)) {
return Collections.singletonList(new FixedHandlerChainCustomizer(new CompletionStageResponseHandler(),
HandlerChainCustomizer.Phase.AFTER_METHOD_INVOKE));
} else if (returnTypeName.equals(UNI)) {
Expand Down

0 comments on commit d8b0dbb

Please sign in to comment.