Skip to content

Always consider @RegisterRestClient interface as REST Clients #47508

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

Merged
merged 1 commit into from
Apr 24, 2025
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 @@ -209,8 +209,10 @@ void processInterfaces(

IndexView index = CompositeIndex.create(beanArchiveIndexBuildItem.getIndex(), combinedIndexBuildItem.getIndex());

findInterfaces(index, interfaces, returnTypes, REGISTER_REST_CLIENT);
findInterfaces(index, interfaces, returnTypes, PATH);
findInterfaces(index, interfaces, returnTypes, REGISTER_REST_CLIENT, classInfo -> true);
// in there, we are overly cautious it could be an interface for a server class
findInterfaces(index, interfaces, returnTypes, PATH,
classInfo -> index.getAllKnownImplementors(classInfo.name()).isEmpty());

if (interfaces.isEmpty()) {
return;
Expand Down Expand Up @@ -380,7 +382,7 @@ private static boolean isDefault(short flags) {
}

private void findInterfaces(IndexView index, Map<DotName, ClassInfo> interfaces, Set<Type> returnTypes,
DotName annotationToFind) {
DotName annotationToFind, Predicate<ClassInfo> additionalConstraints) {
for (AnnotationInstance annotation : index.getAnnotations(annotationToFind)) {
AnnotationTarget target = annotation.target();
ClassInfo theInfo;
Expand All @@ -392,7 +394,7 @@ private void findInterfaces(IndexView index, Map<DotName, ClassInfo> interfaces,
continue;
}

if (!isRestClientInterface(index, theInfo)) {
if (!Modifier.isInterface(theInfo.flags()) || !additionalConstraints.test(theInfo)) {
continue;
}

Expand Down Expand Up @@ -611,12 +613,6 @@ void unremovableInterceptors(List<RestClientBuildItem> restClientInterfaces, Bea
if (!unremovableInterceptors.isEmpty()) {
unremovableBeans.produce(UnremovableBeanBuildItem.beanClassNames(unremovableInterceptors));
}

}

private boolean isRestClientInterface(IndexView index, ClassInfo classInfo) {
return Modifier.isInterface(classInfo.flags())
&& index.getAllKnownImplementors(classInfo.name()).isEmpty();
}

private static BuiltinScope builtinScopeFromName(DotName scopeName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.restclient.basic;

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

import jakarta.enterprise.inject.UnsatisfiedResolutionException;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class ClientWithImplementationsInvalidTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(MyClient.class, MyImplementation.class))
.withConfigurationResource("client-with-implementations.properties")
.assertException(t -> {
assertThat(t).hasCauseInstanceOf(UnsatisfiedResolutionException.class);
});

@RestClient
MyClient client;

@Test
public void testClientBeanHasBeenCreated() {
Assertions.assertEquals("hello", client.get());
}

@Path("/client")
public interface MyClient {

@GET
@Path("/")
String get();
}

public static class MyImplementation implements MyClient {

@GET
@Path("/")
@Override
public String get() {
return "hello";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.quarkus.restclient.basic;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class ClientWithImplementationsTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(MyClient.class, MyImplementation.class))
.withConfigurationResource("client-with-implementations.properties");

@RestClient
MyClient client;

@Test
public void testClientBeanHasBeenCreated() {
Assertions.assertEquals("hello", client.get());
}

@Path("/client")
@RegisterRestClient(configKey = "my-client")
public interface MyClient {

@GET
@Path("/")
String get();
}

public static class MyImplementation implements MyClient {

@GET
@Path("/")
@Override
public String get() {
return "hello";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.rest-client.my-client.url=http://localhost:${quarkus.http.test-port:8081}
Loading