Skip to content

Commit 6e1ed24

Browse files
authored
Merge pull request #52742 from radcortez/fix-52718
Ensure that `@ConfigProperties` are registered before Startup event
2 parents b86f17a + ad88ba2 commit 6e1ed24

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ConfigBuildStep.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import io.quarkus.deployment.builditem.ConfigPropertiesBuildItem;
6666
import io.quarkus.deployment.builditem.ConfigurationBuildItem;
6767
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
68+
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
6869
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
6970
import io.quarkus.deployment.builditem.nativeimage.ReflectiveMethodBuildItem;
7071
import io.quarkus.deployment.pkg.NativeConfig;
@@ -451,15 +452,28 @@ void validateConfigPropertiesInjectionPoints(
451452
toRegister.forEach(configProperties::produce);
452453
}
453454

455+
/**
456+
* Registers the {@link org.eclipse.microprofile.config.inject.ConfigProperties} beans after
457+
* the creation of {@link io.smallrye.config.SmallRyeConfig}. It should be possible to register these in the config
458+
* builder during build time, but unfortunately the MP Config TCK requires to throw a
459+
* {@link jakarta.enterprise.inject.spi.DeploymentException} when a
460+
* {@link org.eclipse.microprofile.config.inject.ConfigProperties} bean cannot be initialized (due to missing
461+
* configuration). When {@link io.smallrye.config.SmallRyeConfig} cannot map a config class, it throws a
462+
* {@link io.smallrye.config.ConfigValidationException}. The recorder catches this exception and throws the
463+
* expected {@link jakarta.enterprise.inject.spi.DeploymentException}.
464+
*/
454465
@BuildStep
455466
@Record(RUNTIME_INIT)
456467
void registerConfigClasses(
457468
RecorderContext context,
458469
ConfigRecorder recorder,
459-
List<ConfigMappingBuildItem> configMappings,
460-
List<ConfigPropertiesBuildItem> configProperties) throws Exception {
470+
List<ConfigPropertiesBuildItem> configProperties,
471+
BuildProducer<ServiceStartBuildItem> serviceStart) throws Exception {
472+
473+
if (configProperties.isEmpty()) {
474+
return;
475+
}
461476

462-
// TODO - Register ConfigProperties during build time
463477
context.registerNonDefaultConstructor(
464478
ConfigClass.class.getDeclaredConstructor(Class.class, String.class),
465479
configClass -> Stream.of(configClass.getType(), configClass.getPrefix())
@@ -469,6 +483,9 @@ void registerConfigClasses(
469483
configProperties.stream()
470484
.map(p -> configClass(p.getConfigClass(), p.getPrefix()))
471485
.collect(toSet()));
486+
487+
// Ensure that @ConfigProperties are registered before Startup events
488+
serviceStart.produce(new ServiceStartBuildItem("microprofile-config-properties"));
472489
}
473490

474491
private static String getPropertyName(String name, ClassInfo declaringClass) {

extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigPropertiesTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.Map;
77

8+
import jakarta.enterprise.context.ApplicationScoped;
89
import jakarta.enterprise.context.Dependent;
910
import jakarta.enterprise.inject.spi.CDI;
1011
import jakarta.inject.Inject;
@@ -14,6 +15,7 @@
1415
import org.junit.jupiter.api.Test;
1516
import org.junit.jupiter.api.extension.RegisterExtension;
1617

18+
import io.quarkus.runtime.Startup;
1719
import io.quarkus.test.QuarkusUnitTest;
1820
import io.smallrye.config.ConfigMapping;
1921
import io.smallrye.config.WithDefault;
@@ -137,6 +139,17 @@ void overridePrefixBean() {
137139
assertEquals(80, client.port());
138140
}
139141

142+
@Inject
143+
StartupBean startupBean;
144+
145+
@Test
146+
void startup() {
147+
ServerConfigProperties server = startupBean.getServer();
148+
assertNotNull(server);
149+
assertEquals("localhost", server.host);
150+
assertEquals(8080, server.port);
151+
}
152+
140153
@ConfigMapping(prefix = "server")
141154
public interface Server {
142155
String host();
@@ -183,4 +196,19 @@ public void setClient(@ConfigMapping(prefix = "client") final Server client) {
183196
this.client = client;
184197
}
185198
}
199+
200+
@ApplicationScoped
201+
@Startup
202+
static class StartupBean {
203+
ServerConfigProperties server;
204+
205+
@Inject
206+
public StartupBean(@ConfigProperties ServerConfigProperties server) {
207+
this.server = server;
208+
}
209+
210+
public ServerConfigProperties getServer() {
211+
return server;
212+
}
213+
}
186214
}

0 commit comments

Comments
 (0)