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 parameter validation issue in classic rest-client #33918

Merged
merged 1 commit into from
Jun 22, 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 @@ -14,7 +14,7 @@ public class ClassicRestClientBuilderFactoryTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClasses(EchoClientWithoutAnnotation.class, EchoClientWithConfigKey.class,
EchoResource.class))
EchoClientWithEmptyPath.class, EchoResource.class))
.withConfigurationResource("factory-test-application.properties");

@Test
Expand All @@ -33,4 +33,13 @@ public void testNotAnnotatedClientClass() {

assertThat(restClient.echo("Hello")).contains("Hello");
}

@Test
public void testEmptyPathAnnotationOnClass() {
RestClientBuilder restClientBuilder = RestClientBuilderFactory.getInstance()
.newBuilder(EchoClientWithEmptyPath.class);
EchoClientWithEmptyPath restClient = restClientBuilder.build(EchoClientWithEmptyPath.class);

assertThat(restClient.echo("echo", "Hello")).contains("Hello");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.restclient.configuration;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@Path("")
@RegisterRestClient(configKey = "echo-client")
public interface EchoClientWithEmptyPath {

@GET
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
@Path("/{id}")
String echo(@PathParam("id") String id, @QueryParam("message") String message);

}
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,12 @@ private <T> void verifyInterface(Class<T> typeDef) {
for (Method method : methods) {
Path methodPathAnno = method.getAnnotation(Path.class);
if (methodPathAnno != null) {
template = classPathAnno == null ? (ResteasyUriBuilder) new ResteasyUriBuilderImpl().uri(methodPathAnno.value())
template = classPathAnno == null
? (ResteasyUriBuilder) new ResteasyUriBuilderImpl().path(methodPathAnno.value())
: (ResteasyUriBuilder) new ResteasyUriBuilderImpl()
.uri(classPathAnno.value() + "/" + methodPathAnno.value());
.path(classPathAnno.value() + "/" + methodPathAnno.value());
} else if (classPathAnno != null) {
template = (ResteasyUriBuilder) new ResteasyUriBuilderImpl().uri(classPathAnno.value());
template = (ResteasyUriBuilder) new ResteasyUriBuilderImpl().path(classPathAnno.value());
} else {
template = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ public class ConfigurationTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar.addClasses(HelloClientWithBaseUri.class, EchoResource.class))
.withApplicationRoot(
jar -> jar.addClasses(HelloClientWithBaseUri.class, EchoResource.class, EchoClientWithEmptyPath.class))
.withConfigurationResource("configuration-test-application.properties");

@RestClient
HelloClientWithBaseUri client;

@RestClient
EchoClientWithEmptyPath echoClientWithEmptyPath;

@Test
void shouldHaveSingletonScope() {
BeanManager beanManager = Arc.container().beanManager();
Expand Down Expand Up @@ -62,6 +66,11 @@ void checkClientSpecificConfigs() {
verifyClientConfig(clientConfig, false);
}

@Test
void emptyPathAnnotationShouldWork() {
assertThat(echoClientWithEmptyPath.echo("hello", "hello world")).isEqualTo("hello world");
}

private void verifyClientConfig(RestClientConfig clientConfig, boolean checkExtraProperties) {
assertThat(clientConfig.url).isPresent();
assertThat(clientConfig.url.get()).endsWith("/hello");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.rest.client.reactive;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@Path("")
@RegisterRestClient
public interface EchoClientWithEmptyPath {
@GET
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
@Path("/{id}")
String echo(@PathParam("id") String id, @QueryParam("message") String message);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.quarkus.rest.client.reactive.configuration;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
Expand All @@ -21,4 +23,9 @@ public String echo(String name, @Context Request request, @Context HttpHeaders h
return (message != null ? message : "hello") + (comma != null ? comma : "_") + " " + name
+ (suffix != null ? suffix : "");
}

@GET
public String echoQueryParam(@QueryParam("message") String message) {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".kee
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".max-redirects=5
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".headers.message=hi
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".headers.suffix=!
quarkus.rest-client."io.quarkus.rest.client.reactive.EchoClientWithEmptyPath".url=http://localhost:${quarkus.http.test-port:8081}/

# client identified by a configKey
quarkus.rest-client.client-prefix.url=http://localhost:${quarkus.http.test-port:8081}/hello
Expand Down