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

Register REST Client body parameters for reflection #30132

Merged
merged 1 commit into from
Jan 5, 2023
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 @@ -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