Skip to content

Commit 4869013

Browse files
committed
RestClient processor with NullPointerException during scope definition
1 parent 8ea2b28 commit 4869013

File tree

5 files changed

+98
-9
lines changed

5 files changed

+98
-9
lines changed

extensions/resteasy-classic/rest-client-config/deployment/src/main/java/io/quarkus/restclient/config/deployment/RestClientsBuildTimeConfigBuildItem.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.quarkus.restclient.config.RegisteredRestClient;
2121
import io.quarkus.restclient.config.RestClientKeysProvider;
2222
import io.quarkus.restclient.config.RestClientsBuildTimeConfig;
23+
import io.quarkus.runtime.configuration.ConfigurationException;
2324
import io.smallrye.config.ConfigValue;
2425
import io.smallrye.config.DefaultValuesConfigSource;
2526
import io.smallrye.config.SmallRyeConfig;
@@ -105,7 +106,8 @@ public Optional<BuiltinScope> getScope(final Capabilities capabilities, final Cl
105106

106107
// First config in the rest client service
107108
restClientsBuildTimeConfig.clients().get(restClientInterface.name().toString()).scope()
108-
.ifPresent(s -> discoveredScopes.add(Optional.of(builtinScopeFromName(DotName.createSimple(s)))));
109+
.ifPresent(s -> discoveredScopes
110+
.add(builtinScopeFromName(DotName.createSimple(s), restClientInterface.name().toString())));
109111

110112
// Second annotation in the rest client declaration
111113
Set<DotName> annotations = restClientInterface.annotationsMap().keySet();
@@ -118,7 +120,8 @@ public Optional<BuiltinScope> getScope(final Capabilities capabilities, final Cl
118120

119121
// Third global config
120122
restClientsBuildTimeConfig.scope()
121-
.ifPresent(s -> discoveredScopes.add(Optional.of(builtinScopeFromName(DotName.createSimple(s)))));
123+
.ifPresent(s -> discoveredScopes
124+
.add(builtinScopeFromName(DotName.createSimple(s), restClientInterface.name().toString())));
122125

123126
Optional<BuiltinScope> scope = discoveredScopes.stream()
124127
.filter(Optional::isPresent)
@@ -136,15 +139,18 @@ public Optional<BuiltinScope> getScope(final Capabilities capabilities, final Cl
136139
return Optional.empty();
137140
}
138141

139-
private static BuiltinScope builtinScopeFromName(DotName scopeName) {
142+
private static Optional<BuiltinScope> builtinScopeFromName(DotName scopeName, String restClientClass) {
140143
BuiltinScope scope = BuiltinScope.from(scopeName);
141-
if (scope == null) {
142-
for (BuiltinScope builtinScope : BuiltinScope.values()) {
143-
if (builtinScope.getName().withoutPackagePrefix().equalsIgnoreCase(scopeName.toString())) {
144-
scope = builtinScope;
145-
}
144+
if (scope != null) {
145+
return Optional.of(scope);
146+
}
147+
148+
for (BuiltinScope builtinScope : BuiltinScope.values()) {
149+
if (builtinScope.getName().withoutPackagePrefix().equalsIgnoreCase(scopeName.toString())) {
150+
return Optional.of(builtinScope);
146151
}
147152
}
148-
return scope;
153+
throw new ConfigurationException(
154+
String.format("Not possible to define the scope %s for the REST client %s ", scopeName, restClientClass));
149155
}
150156
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.quarkus.restclient.configuration;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.fail;
5+
6+
import jakarta.ws.rs.GET;
7+
import jakarta.ws.rs.Path;
8+
9+
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
10+
import org.eclipse.microprofile.rest.client.inject.RestClient;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.extension.RegisterExtension;
13+
14+
import io.quarkus.test.QuarkusUnitTest;
15+
16+
public class ClientWithWrongScopeTest {
17+
18+
@RegisterExtension
19+
static final QuarkusUnitTest config = new QuarkusUnitTest()
20+
.withApplicationRoot((jar) -> jar
21+
.addClasses(MyClient.class))
22+
.withConfigurationResource("client-with-wrong-scope.properties")
23+
.assertException(t -> assertThat(t).hasMessageContaining("Not possible to define the scope"));
24+
25+
@RestClient
26+
MyClient client;
27+
28+
@Test
29+
public void testValidationFailed() {
30+
// This method should not be invoked
31+
fail();
32+
}
33+
34+
@Path("/client")
35+
@RegisterRestClient(configKey = "my-client")
36+
public interface MyClient {
37+
38+
@GET
39+
@Path("/")
40+
String get();
41+
}
42+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
my-client/mp-rest/url=http://localhost:${quarkus.http.test-port:8081}
2+
my-client/mp-rest/scope=wrong-scope
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.quarkus.rest.client.reactive;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.fail;
5+
6+
import jakarta.ws.rs.GET;
7+
import jakarta.ws.rs.Path;
8+
9+
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.RegisterExtension;
12+
13+
import io.quarkus.test.QuarkusUnitTest;
14+
15+
public class ClientWithWrongScopeTest {
16+
@RegisterExtension
17+
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
18+
.withApplicationRoot((jar) -> jar
19+
.addClasses(MyClient.class))
20+
.withConfigurationResource("wrong-scope-test-application.properties")
21+
.assertException(t -> assertThat(t).hasMessageContaining("Not possible to define the scope"));
22+
23+
@Test
24+
public void testValidationFailed() {
25+
// This method should not be invoked
26+
fail();
27+
}
28+
29+
@Path("/client")
30+
@RegisterRestClient(configKey = "my-client")
31+
public interface MyClient {
32+
33+
@GET
34+
@Path("/")
35+
String get();
36+
}
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
my-client/mp-rest/url=http://localhost:${quarkus.http.test-port:8081}
2+
my-client/mp-rest/scope=wrong-scope

0 commit comments

Comments
 (0)