Skip to content

Commit

Permalink
Fix Uni response type handling in quarkus-rest-client-reactive
Browse files Browse the repository at this point in the history
Uni was not being treated as an async return type resulting in
the client making blocking calls and trying to deserialize the
result into a Uni

Fixes: quarkusio#16148
  • Loading branch information
geoand committed Apr 1, 2021
1 parent 1ccf0d0 commit 8977265
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.quarkus.deployment.Feature.RESTEASY_REACTIVE_JAXRS_CLIENT;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COMPLETION_STAGE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.UNI;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.WEB_APPLICATION_EXCEPTION;

import java.io.Closeable;
Expand All @@ -25,6 +26,7 @@
import javax.ws.rs.client.CompletionStageRxInvoker;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.RxInvoker;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
Expand All @@ -38,6 +40,7 @@
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.client.impl.AsyncInvokerImpl;
import org.jboss.resteasy.reactive.client.impl.ClientImpl;
import org.jboss.resteasy.reactive.client.impl.UniInvoker;
import org.jboss.resteasy.reactive.client.impl.WebTargetImpl;
import org.jboss.resteasy.reactive.common.core.GenericTypeMapping;
import org.jboss.resteasy.reactive.common.core.ResponseBuilderFactory;
Expand Down Expand Up @@ -100,6 +103,7 @@
import io.quarkus.resteasy.reactive.spi.MessageBodyWriterBuildItem;
import io.quarkus.resteasy.reactive.spi.MessageBodyWriterOverrideBuildItem;
import io.quarkus.runtime.RuntimeValue;
import io.smallrye.mutiny.Uni;

public class JaxrsClientProcessor {

Expand Down Expand Up @@ -525,15 +529,17 @@ public void close() {
}

Type returnType = jandexMethod.returnType();
boolean completionStage = false;
String simpleReturnType = method.getSimpleReturnType();
ReturnCategory returnCategory = ReturnCategory.BLOCKING;

String simpleReturnType = method.getSimpleReturnType();
ResultHandle genericReturnType = null;

if (returnType.kind() == Type.Kind.PARAMETERIZED_TYPE) {

ParameterizedType paramType = returnType.asParameterizedType();
if (paramType.name().equals(COMPLETION_STAGE)) {
completionStage = true;
if (paramType.name().equals(COMPLETION_STAGE) || paramType.name().equals(UNI)) {
returnCategory = paramType.name().equals(COMPLETION_STAGE) ? ReturnCategory.COMPLETION_STAGE
: ReturnCategory.UNI;

// CompletionStage has one type argument:
if (paramType.arguments().isEmpty()) {
Expand Down Expand Up @@ -600,7 +606,7 @@ public void close() {
tryBlock.getMethodParam(bodyParameterIdx),
mediaType);

if (completionStage) {
if (returnCategory == ReturnCategory.COMPLETION_STAGE) {
ResultHandle async = tryBlock.invokeInterfaceMethod(
MethodDescriptor.ofMethod(Invocation.Builder.class, "async", AsyncInvoker.class),
builder);
Expand All @@ -620,6 +626,27 @@ public void close() {
async, tryBlock.load(method.getHttpMethod()), entity,
tryBlock.loadClass(simpleReturnType));
}
} else if (returnCategory == ReturnCategory.UNI) {
ResultHandle rx = tryBlock.invokeInterfaceMethod(
MethodDescriptor.ofMethod(Invocation.Builder.class, "rx", RxInvoker.class, Class.class),
builder, tryBlock.loadClass(UniInvoker.class));
ResultHandle uniInvoker = tryBlock.checkCast(rx, UniInvoker.class);
// with entity
if (genericReturnType != null) {
result = tryBlock.invokeVirtualMethod(
MethodDescriptor.ofMethod(UniInvoker.class, "method",
Uni.class, String.class,
Entity.class, GenericType.class),
uniInvoker, tryBlock.load(method.getHttpMethod()), entity,
genericReturnType);
} else {
result = tryBlock.invokeVirtualMethod(
MethodDescriptor.ofMethod(UniInvoker.class, "method",
Uni.class, String.class,
Entity.class, Class.class),
uniInvoker, tryBlock.load(method.getHttpMethod()), entity,
tryBlock.loadClass(simpleReturnType));
}
} else {
if (genericReturnType != null) {
result = tryBlock.invokeInterfaceMethod(
Expand All @@ -637,7 +664,7 @@ public void close() {
}
} else {

if (completionStage) {
if (returnCategory == ReturnCategory.COMPLETION_STAGE) {
ResultHandle async = tryBlock.invokeInterfaceMethod(
MethodDescriptor.ofMethod(Invocation.Builder.class, "async", AsyncInvoker.class),
builder);
Expand All @@ -655,6 +682,25 @@ public void close() {
async, tryBlock.load(method.getHttpMethod()),
tryBlock.loadClass(simpleReturnType));
}
} else if (returnCategory == ReturnCategory.UNI) {
ResultHandle rx = tryBlock.invokeInterfaceMethod(
MethodDescriptor.ofMethod(Invocation.Builder.class, "rx", RxInvoker.class, Class.class),
builder, tryBlock.loadClass(UniInvoker.class));
ResultHandle uniInvoker = tryBlock.checkCast(rx, UniInvoker.class);
if (genericReturnType != null) {
result = tryBlock.invokeVirtualMethod(
MethodDescriptor.ofMethod(UniInvoker.class, "method",
Uni.class, String.class,
GenericType.class),
uniInvoker, tryBlock.load(method.getHttpMethod()), genericReturnType);
} else {
result = tryBlock.invokeVirtualMethod(
MethodDescriptor.ofMethod(UniInvoker.class, "method",
Uni.class, String.class,
Class.class),
uniInvoker, tryBlock.load(method.getHttpMethod()),
tryBlock.loadClass(simpleReturnType));
}
} else {
if (genericReturnType != null) {
result = tryBlock.invokeInterfaceMethod(
Expand Down Expand Up @@ -830,4 +876,10 @@ private void addCookieParam(BytecodeCreator invoBuilderEnricher, AssignableResul
invocationBuilder, invoBuilderEnricher.load(paramName), cookieParamHandle));
}

private enum ReturnCategory {
BLOCKING,
COMPLETION_STAGE,
UNI
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkus.it.rest.client;

import java.net.URI;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

import javax.enterprise.context.ApplicationScoped;
Expand All @@ -13,14 +12,16 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

import io.smallrye.mutiny.Uni;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;

@ApplicationScoped
public class ClientCallingResource {
private static final ObjectMapper mapper = new JsonMapper();

private static final String[] RESPONSES = { "cortland", "cortland2" };
private static final String[] RESPONSES = { "cortland", "cortland2", "cortland3" };
private final AtomicInteger count = new AtomicInteger(0);

void init(@Observes Router router) {
Expand All @@ -36,20 +37,41 @@ void init(@Observes Router router) {
String url = rc.getBody().toString();
SimpleClient client = RestClientBuilder.newBuilder().baseUri(URI.create(url))
.build(SimpleClient.class);
Apple swappedApple = client.swapApple(new Apple("lobo"));
client.completionApple(new Apple("lobo2")).whenComplete((apple2, t) -> {
if (t == null) {
try {
rc.response().putHeader("content-type", "application/json")
.end(mapper.writeValueAsString(Arrays.asList(swappedApple, apple2)));
return;
} catch (JsonProcessingException e) {
t = e;
}
}
rc.response().putHeader("content-type", "text/plain").setStatusCode(500).end(t.getMessage());
});
Uni<Apple> apple1 = Uni.createFrom().item(client.swapApple(new Apple("lobo")));
Uni<Apple> apple2 = Uni.createFrom().completionStage(client.completionSwapApple(new Apple("lobo2")));
Uni<Apple> apple3 = client.uniSwapApple(new Apple("lobo3"));
Uni<Apple> apple4 = Uni.createFrom().item(client.someApple());
Uni<Apple> apple5 = Uni.createFrom().completionStage(client.completionSomeApple());
Uni<Apple> apple6 = client.uniSomeApple();
Uni<Apple> apple7 = Uni.createFrom().item(client.stringApple()).onItem().transform(this::toApple);
Uni<Apple> apple8 = Uni.createFrom().completionStage(client.completionStringApple()).onItem()
.transform(this::toApple);
Uni<Apple> apple9 = client.uniStringApple().onItem().transform(this::toApple);
Uni.combine().all().unis(apple1, apple2, apple3, apple4, apple5, apple6, apple7, apple8, apple9).asTuple()
.subscribe()
.with(tuple -> {
try {
rc.response().putHeader("content-type", "application/json")
.end(mapper.writeValueAsString(tuple.asList()));
} catch (JsonProcessingException e) {
fail(rc, e.getMessage());
}
}, t -> {
fail(rc, t.getMessage());
});
});
}

private void fail(RoutingContext rc, String message) {
rc.response().putHeader("content-type", "text/plain").setStatusCode(500).end(message);
}

private Apple toApple(String s) {
try {
return mapper.readValue(s, Apple.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,46 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import io.smallrye.mutiny.Uni;

@Path("")
public interface SimpleClient {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Apple swapApple(Apple original);

@POST
@Produces(MediaType.APPLICATION_JSON)
Apple someApple();

@POST
@Produces(MediaType.APPLICATION_JSON)
String stringApple();

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
CompletionStage<Apple> completionApple(Apple original);
CompletionStage<Apple> completionSwapApple(Apple original);

@POST
@Produces(MediaType.APPLICATION_JSON)
CompletionStage<Apple> completionSomeApple();

@POST
@Produces(MediaType.APPLICATION_JSON)
CompletionStage<String> completionStringApple();

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Uni<Apple> uniSwapApple(Apple original);

@POST
@Produces(MediaType.APPLICATION_JSON)
Uni<Apple> uniSomeApple();

@POST
@Produces(MediaType.APPLICATION_JSON)
Uni<String> uniStringApple();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ public void shouldWork() {
.then()
.statusCode(200)
.contentType("application/json")
.body("size()", is(2))
.body("size()", is(9))
.body("[0].cultivar", equalTo("cortland"))
.body("[1].cultivar", equalTo("cortland2"));
.body("[1].cultivar", equalTo("cortland2"))
.body("[2].cultivar", equalTo("cortland3"))
.body("[3].cultivar", equalTo("cortland"))
.body("[4].cultivar", equalTo("cortland2"))
.body("[5].cultivar", equalTo("cortland3"))
.body("[6].cultivar", equalTo("cortland"))
.body("[7].cultivar", equalTo("cortland2"))
.body("[8].cultivar", equalTo("cortland3"));
}
}

0 comments on commit 8977265

Please sign in to comment.