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

Rest Client Reactive: Support PathParam from BeanParam #20056

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 @@ -66,6 +66,7 @@
import org.jboss.resteasy.reactive.client.processor.beanparam.CookieParamItem;
import org.jboss.resteasy.reactive.client.processor.beanparam.HeaderParamItem;
import org.jboss.resteasy.reactive.client.processor.beanparam.Item;
import org.jboss.resteasy.reactive.client.processor.beanparam.PathParamItem;
import org.jboss.resteasy.reactive.client.processor.beanparam.QueryParamItem;
import org.jboss.resteasy.reactive.client.processor.scanning.ClientEndpointIndexer;
import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
Expand Down Expand Up @@ -486,7 +487,7 @@ A more full example of generated client (with sub-resource) can is at the bottom
methodIndex++;

// finding corresponding jandex method, used by enricher (MicroProfile enricher stores it in a field
// to later fill in context with corresponding java.lang.reflect.Method
// to later fill in context with corresponding java.lang.reflect.Method)
String[] javaMethodParameters = new String[method.getParameters().length];
for (int i = 0; i < method.getParameters().length; i++) {
MethodParameter param = method.getParameters()[i];
Expand Down Expand Up @@ -1636,6 +1637,12 @@ private void addBeanParamData(BytecodeCreator methodCreator,
headerParam.getHeaderName(),
headerParam.extract(invoEnricher, invoEnricher.getMethodParam(1)));
break;
case PATH_PARAM:
PathParamItem pathParam = (PathParamItem) item;
addPathParam(creator, target,
pathParam.getPathParamName(),
pathParam.extract(creator, param));
break;
default:
throw new IllegalStateException("Unimplemented"); // TODO form params, etc
}
Expand Down Expand Up @@ -1688,6 +1695,14 @@ private void addHeaderParam(BytecodeCreator invoBuilderEnricher, AssignableResul
invocationBuilder, invoBuilderEnricher.load(paramName), headerParamHandle));
}

private void addPathParam(BytecodeCreator methodCreator, AssignableResultHandle methodTarget,
String paramName, ResultHandle pathParamHandle) {
methodCreator.assign(methodTarget,
methodCreator.invokeInterfaceMethod(WEB_TARGET_RESOLVE_TEMPLATE_METHOD,
methodTarget,
methodCreator.load(paramName), pathParamHandle));
}

private void addCookieParam(BytecodeCreator invoBuilderEnricher, AssignableResultHandle invocationBuilder,
String paramName, ResultHandle cookieParamHandle) {
invoBuilderEnricher.assign(invocationBuilder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.quarkus.rest.client.reactive.beanparam;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;

import javax.ws.rs.BeanParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;

public class BeanPathParamTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest();

@TestHTTPResource
URI baseUri;

@Test
void shouldPassPathParamFromBeanParam() {
Client client = RestClientBuilder.newBuilder().baseUri(baseUri).build(Client.class);
assertThat(client.getWithBeanParam(new MyBeanParam("123"))).isEqualTo("it works!");
}

@Test
void shouldPassPathParamFromBeanParamAndMethod() {
Client client = RestClientBuilder.newBuilder().baseUri(baseUri).build(Client.class);
assertThat(client.getWithBeanParam("foo", new MyBeanParam("123"))).isEqualTo("it works with method too!");
}

@Path("/my/{id}/resource")
public interface Client {
@GET
String getWithBeanParam(@BeanParam MyBeanParam beanParam);

@GET
@Path("/{name}")
String getWithBeanParam(@PathParam("name") String name, @BeanParam MyBeanParam beanParam);
}

public static class MyBeanParam {
private final String id;

public MyBeanParam(String id) {
this.id = id;
}

@PathParam("id")
public String getId() {
return id;
}
}

@Path("/my/123/resource")
public static class Resource {
@GET
public String get() {
return "it works!";
}

@Path("/foo")
@GET
public String getWithLongerPath() {
return "it works with method too!";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.BEAN_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COOKIE_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.HEADER_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.PATH_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.QUERY_PARAM;

import java.util.ArrayList;
Expand Down Expand Up @@ -81,15 +82,31 @@ public static List<Item> parse(ClassInfo beanParamClass, IndexView index) {

List<AnnotationInstance> headerParams = annotations.get(HEADER_PARAM);
if (headerParams != null) {
for (AnnotationInstance queryParamAnnotation : headerParams) {
AnnotationTarget target = queryParamAnnotation.target();
for (AnnotationInstance headerParamAnnotation : headerParams) {
AnnotationTarget target = headerParamAnnotation.target();
if (target.kind() == AnnotationTarget.Kind.FIELD) {
FieldInfo fieldInfo = target.asField();
resultList.add(new HeaderParamItem(queryParamAnnotation.value().asString(),
resultList.add(new HeaderParamItem(headerParamAnnotation.value().asString(),
new FieldExtractor(null, fieldInfo.name(), fieldInfo.declaringClass().name().toString())));
} else if (target.kind() == AnnotationTarget.Kind.METHOD) {
MethodInfo getterMethod = getGetterMethod(beanParamClass, target.asMethod());
resultList.add(new HeaderParamItem(queryParamAnnotation.value().asString(),
resultList.add(new HeaderParamItem(headerParamAnnotation.value().asString(),
new GetterExtractor(getterMethod)));
}
}
}

List<AnnotationInstance> pathParams = annotations.get(PATH_PARAM);
if (pathParams != null) {
for (AnnotationInstance pathParamAnnotation : pathParams) {
AnnotationTarget target = pathParamAnnotation.target();
if (target.kind() == AnnotationTarget.Kind.FIELD) {
FieldInfo fieldInfo = target.asField();
resultList.add(new PathParamItem(pathParamAnnotation.value().asString(),
new FieldExtractor(null, fieldInfo.name(), fieldInfo.declaringClass().name().toString())));
} else if (target.kind() == AnnotationTarget.Kind.METHOD) {
MethodInfo getterMethod = getGetterMethod(beanParamClass, target.asMethod());
resultList.add(new PathParamItem(pathParamAnnotation.value().asString(),
new GetterExtractor(getterMethod)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public enum ItemType {
QUERY_PARAM,
COOKIE,
HEADER_PARAM,
PATH_PARAM,
// TODO: more
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jboss.resteasy.reactive.client.processor.beanparam;

public class PathParamItem extends Item {

private final String pathParamName;

public PathParamItem(String pathParamName, ValueExtractor valueExtractor) {
super(ItemType.PATH_PARAM, valueExtractor);
this.pathParamName = pathParamName;
}

public String getPathParamName() {
return pathParamName;
}
}