Skip to content

Commit

Permalink
Register REST Client body parameters for reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jan 3, 2023
1 parent dfc0b63 commit ca03e79
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,28 @@ public void accept(EndpointIndexer.ResourceMethodCallbackData entry) {
QuarkusResteasyReactiveDotNames.IGNORE_METHOD_FOR_REFLECTION_PREDICATE)
.source(source)
.build());

// if there is a body parameter type, register it for reflection
ResourceMethod resourceMethod = entry.getResourceMethod();
MethodParameter[] methodParameters = resourceMethod.getParameters();
if (methodParameters != null) {
for (int i = 0; i < methodParameters.length; i++) {
MethodParameter methodParameter = methodParameters[i];
if (methodParameter.getParameterType() == ParameterType.BODY) {
reflectiveHierarchyBuildItemBuildProducer.produce(new ReflectiveHierarchyBuildItem.Builder()
.type(method.parameterType(i))
.index(index)
.ignoreTypePredicate(
QuarkusResteasyReactiveDotNames.IGNORE_TYPE_FOR_REFLECTION_PREDICATE)
.ignoreFieldPredicate(
QuarkusResteasyReactiveDotNames.IGNORE_FIELD_FOR_REFLECTION_PREDICATE)
.ignoreMethodPredicate(
QuarkusResteasyReactiveDotNames.IGNORE_METHOD_FOR_REFLECTION_PREDICATE)
.source(source)
.build());
}
}
}
}
})
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ void init(@Observes Router router) {
router.post("/hello").handler(rc -> rc.response().putHeader("content-type", MediaType.TEXT_PLAIN)
.end("Hello, " + (rc.getBodyAsString()).repeat(getCount(rc))));

router.post("/hello/fromMessage").handler(rc -> rc.response().putHeader("content-type", MediaType.TEXT_PLAIN)
.end(rc.body().asJsonObject().getString("message")));

router.route("/call-hello-client").blockingHandler(rc -> {
String url = rc.getBody().toString();
HelloClient client = RestClientBuilder.newBuilder().baseUri(URI.create(url))
Expand All @@ -126,6 +129,14 @@ void init(@Observes Router router) {
rc.response().end(greeting);
});

router.route("/call-helloFromMessage-client").blockingHandler(rc -> {
String url = rc.getBody().toString();
HelloClient client = RestClientBuilder.newBuilder().baseUri(URI.create(url))
.build(HelloClient.class);
String greeting = client.fromMessage(new HelloClient.Message("Hello world"));
rc.response().end(greeting);
});

router.post("/params/param").handler(rc -> rc.response().putHeader("content-type", MediaType.TEXT_PLAIN)
.end(getParam(rc)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fasterxml.jackson.annotation.JsonCreator;

import io.quarkus.rest.client.reactive.ClientExceptionMapper;

@Path("")
Expand All @@ -18,6 +20,12 @@ public interface HelloClient {
@Consumes(MediaType.TEXT_PLAIN)
String greeting(String name, @QueryParam("count") int count);

@Path("fromMessage")
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
String fromMessage(Message message);

// this isn't used, but it makes sure that the generated provider can be properly instantiated in native mode
@ClientExceptionMapper
static RuntimeException toException(Response response) {
Expand All @@ -26,4 +34,18 @@ static RuntimeException toException(Response response) {
}
return null;
}

class Message {

private final String message;

@JsonCreator
public Message(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public void shouldMakeTextRequest() {
assertThat(response.asString()).isEqualTo("Hello, JohnJohn");
}

@Test
public void shouldMakeJsonRequestAndGetTextResponse() {
Response response = RestAssured.with().body(helloUrl).post("/call-helloFromMessage-client");
assertThat(response.asString()).isEqualTo("Hello world");
}

@Test
public void restResponseShouldWorkWithNonSuccessfulResponse() {
Response response = RestAssured.with().body(helloUrl).post("/rest-response");
Expand Down

0 comments on commit ca03e79

Please sign in to comment.