-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make REST Client a bean when the scope property is set in config
Closes: #37007
- Loading branch information
Showing
9 changed files
with
138 additions
and
18 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...ient-config/runtime/src/main/java/io/quarkus/restclient/config/RestClientBuildConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.restclient.config; | ||
|
||
import java.util.Optional; | ||
|
||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
@ConfigGroup | ||
public class RestClientBuildConfig { | ||
|
||
/** | ||
* The CDI scope to use for injection. This property can contain either a fully qualified class name of a CDI scope | ||
* annotation (such as "jakarta.enterprise.context.ApplicationScoped") or its simple name (such as | ||
* "ApplicationScoped"). | ||
* By default, this is not set which means the interface is not registered as a bean unless it is annotated with {@link RegisterRestClient}. | ||
* If an interface is not annotated with {@link RegisterRestClient} and this property is set, then Quarkus will make the interface | ||
* a bean of the configured scope. | ||
*/ | ||
@ConfigItem | ||
public Optional<String> scope; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...config/runtime/src/main/java/io/quarkus/restclient/config/RestClientsBuildTimeConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.quarkus.restclient.config; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import io.quarkus.runtime.annotations.ConfigItem; | ||
import io.quarkus.runtime.annotations.ConfigPhase; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
|
||
@ConfigRoot(name = "rest-client", phase = ConfigPhase.BUILD_TIME) | ||
public class RestClientsBuildTimeConfig { | ||
|
||
/** | ||
* Configurations of REST client instances. | ||
*/ | ||
@ConfigItem(name = ConfigItem.PARENT) | ||
public Map<String, RestClientBuildConfig> configs = Collections.emptyMap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...reactive/deployment/src/test/java/io/quarkus/rest/client/reactive/BeanFromConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.quarkus.rest.client.reactive; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Set; | ||
|
||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.spi.Bean; | ||
import jakarta.enterprise.inject.spi.BeanManager; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class BeanFromConfigTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(HelloClient.class, HelloResource.class)) | ||
.overrideConfigKey("quarkus.rest-client.\"io.quarkus.rest.client.reactive.HelloClient\".scope", "Dependent") | ||
.overrideRuntimeConfigKey("quarkus.rest-client.\"io.quarkus.rest.client.reactive.HelloClient\".url", | ||
"http://localhost:${quarkus.http.test-port:8081}"); | ||
|
||
@RestClient | ||
HelloClient client; | ||
|
||
@Test | ||
void shouldHello() { | ||
assertThat(client.echo("w0rld")).isEqualTo("hello, w0rld"); | ||
} | ||
|
||
@Test | ||
void shouldHaveDependentScope() { | ||
BeanManager beanManager = Arc.container().beanManager(); | ||
Set<Bean<?>> beans = beanManager.getBeans(HelloClient.class, RestClient.LITERAL); | ||
Bean<?> resolvedBean = beanManager.resolve(beans); | ||
assertThat(resolvedBean.getScope()).isEqualTo(Dependent.class); | ||
} | ||
|
||
@Path("/hello") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@Consumes(MediaType.TEXT_PLAIN) | ||
public static class HelloResource { | ||
|
||
@POST | ||
public String echo(String name) { | ||
return "hello, " + name; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters