Skip to content

Commit

Permalink
Rest Client - support injection for providers used in @RegisterProvider
Browse files Browse the repository at this point in the history
- i.e. for providers not annotated with @Provider
- resolves quarkusio#7531
- follows up on quarkusio#5752
  • Loading branch information
mkouba committed Mar 4, 2020
1 parent f87c445 commit 9bd8569
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 0 deletions.
5 changes: 5 additions & 0 deletions extensions/rest-client/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
<artifactId>quarkus-smallrye-fault-tolerance-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@ void registerProviders(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
}
}

@BuildStep
AdditionalBeanBuildItem registerProviderBeans(CombinedIndexBuildItem combinedIndex) {
IndexView index = combinedIndex.getIndex();
List<AnnotationInstance> allInstances = new ArrayList<>(index.getAnnotations(REGISTER_PROVIDER));
for (AnnotationInstance annotation : index.getAnnotations(REGISTER_PROVIDERS)) {
allInstances.addAll(Arrays.asList(annotation.value().asNestedArray()));
}
AdditionalBeanBuildItem.Builder builder = AdditionalBeanBuildItem.builder().setUnremovable();
for (AnnotationInstance annotationInstance : allInstances) {
// Make sure all providers not annotated with @Provider but used in @RegisterProvider are registered as beans
builder.addBeanClass(annotationInstance.value().asClass().toString());
}
return builder.build();
}

private boolean isRestClientInterface(IndexView index, ClassInfo classInfo) {
return Modifier.isInterface(classInfo.flags())
&& index.getAllKnownImplementors(classInfo.name()).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.restclient.registerprovider;

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

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

@Path("/echo")
@RegisterRestClient
@RegisterProvider(MyFilter.class)
public interface EchoClient {

@GET
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
String echo(@QueryParam("message") String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.restclient.registerprovider;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Path("/echo")
public class EchoResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String echo(@QueryParam("message") String message) {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.restclient.registerprovider;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.inject.Singleton;

@Singleton
public class MethodsCollector {

private final List<String> methods = new CopyOnWriteArrayList<>();

void collect(String method) {
methods.add(method);
}

List<String> getMethods() {
return methods;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.restclient.registerprovider;

import java.io.IOException;

import javax.inject.Inject;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;

public class MyFilter implements ClientRequestFilter {

@Inject
MethodsCollector collector;

@Override
public void filter(ClientRequestContext requestContext) throws IOException {
collector.collect(requestContext.getMethod());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.quarkus.restclient.registerprovider;

import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.inject.Inject;

import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class RegisterProviderTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(EchoResource.class, EchoClient.class, MyFilter.class, MethodsCollector.class)
.addAsResource(new StringAsset("io.quarkus.restclient.registerprovider.EchoClient/mp-rest/url=${test.url}"),
"application.properties"));

@RestClient
EchoClient client;

@Inject
MethodsCollector collector;

@Test
public void testProvider() {
collector.getMethods().clear();
assertEquals("ping", client.echo("ping"));
assertEquals(1, collector.getMethods().size());
assertEquals("GET", collector.getMethods().get(0));
}

}

0 comments on commit 9bd8569

Please sign in to comment.