Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CompletionStage response type handling in quarkus-rest-client-reactive #16156

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -541,24 +541,14 @@ public void close() {
} else {
Type type = paramType.arguments().get(0);
if (type.kind() == Type.Kind.PARAMETERIZED_TYPE) {
ResultHandle currentThread = methodCreator
.invokeStaticMethod(MethodDescriptors.THREAD_CURRENT_THREAD);
ResultHandle tccl = methodCreator.invokeVirtualMethod(MethodDescriptors.THREAD_GET_TCCL,
currentThread);
genericReturnType = Types.getParameterizedType(methodCreator, tccl, type.asParameterizedType());
genericReturnType = createGenericTypeFromParameterizedType(methodCreator,
type.asParameterizedType());
} else {
simpleReturnType = type.toString();
}
}
} else {
ResultHandle currentThread = methodCreator.invokeStaticMethod(MethodDescriptors.THREAD_CURRENT_THREAD);
ResultHandle tccl = methodCreator.invokeVirtualMethod(MethodDescriptors.THREAD_GET_TCCL, currentThread);
ResultHandle parameterizedType = Types.getParameterizedType(methodCreator, tccl,
paramType);

genericReturnType = methodCreator.newInstance(
MethodDescriptor.ofConstructor(GenericType.class, java.lang.reflect.Type.class),
parameterizedType);
genericReturnType = createGenericTypeFromParameterizedType(methodCreator, paramType);
}
}

Expand Down Expand Up @@ -708,6 +698,18 @@ public void close() {

}

private ResultHandle createGenericTypeFromParameterizedType(MethodCreator methodCreator,
ParameterizedType parameterizedType2) {
ResultHandle genericReturnType;
ResultHandle currentThread = methodCreator.invokeStaticMethod(MethodDescriptors.THREAD_CURRENT_THREAD);
ResultHandle tccl = methodCreator.invokeVirtualMethod(MethodDescriptors.THREAD_GET_TCCL, currentThread);
ResultHandle parameterizedType = Types.getParameterizedType(methodCreator, tccl, parameterizedType2);
genericReturnType = methodCreator.newInstance(
MethodDescriptor.ofConstructor(GenericType.class, java.lang.reflect.Type.class),
parameterizedType);
return genericReturnType;
}

private AssignableResultHandle createWebTargetForMethod(MethodCreator constructor, AssignableResultHandle baseTarget,
ResourceMethod method) {
AssignableResultHandle target = constructor.createVariable(WebTarget.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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;
import javax.enterprise.event.Observes;
Expand All @@ -18,26 +20,36 @@
public class ClientCallingResource {
private static final ObjectMapper mapper = new JsonMapper();

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

void init(@Observes Router router) {
router.post().handler(BodyHandler.create());

router.post("/apples").handler(rc -> rc.response().putHeader("content-type", "application/json")
.end("{\"cultivar\": \"cortland\"}"));
router.post("/apples").handler(rc -> {
int count = this.count.getAndIncrement();
rc.response().putHeader("content-type", "application/json")
.end(String.format("{\"cultivar\": \"%s\"}", RESPONSES[count % RESPONSES.length]));
});

router.route("/call-client").blockingHandler(rc -> {
String url = rc.getBody().toString();
SimpleClient client = RestClientBuilder.newBuilder().baseUri(URI.create(url))
.build(SimpleClient.class);
Apple swappedApple = client.swapApple(new Apple("lobo"));
rc.response().end(jsonAsString(swappedApple));
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());
});
});
}

private String jsonAsString(Apple apple) {
try {
return mapper.writerFor(Apple.class).writeValueAsString(apple);
} catch (JsonProcessingException e) {
throw new RuntimeException("Unable to stringify apple", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.it.rest.client;

import java.util.concurrent.CompletionStage;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand All @@ -12,4 +14,9 @@ public interface SimpleClient {
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Apple swapApple(Apple original);

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
CompletionStage<Apple> completionApple(Apple original);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.quarkus.it.rest.client;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.equalTo;

import org.junit.jupiter.api.Test;

import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.response.Response;

@QuarkusTest
public class BasicTest {
Expand All @@ -17,7 +17,12 @@ public class BasicTest {

@Test
public void shouldWork() {
Response response = RestAssured.with().body(appleUrl).post("/call-client");
assertThat(response.jsonPath().getString("cultivar")).isEqualTo("cortland");
RestAssured.with().body(appleUrl).post("/call-client")
.then()
.statusCode(200)
.contentType("application/json")
.body("size()", is(2))
.body("[0].cultivar", equalTo("cortland"))
.body("[1].cultivar", equalTo("cortland2"));
}
}