Skip to content

Commit

Permalink
Add validation for runtime within deployment modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Nov 30, 2024
1 parent 4221467 commit 71047bd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import io.quarkus.annotation.processor.documentation.config.discovery.EnumDefinition;
import io.quarkus.annotation.processor.documentation.config.discovery.EnumDefinition.EnumConstant;
import io.quarkus.annotation.processor.documentation.config.discovery.ResolvedType;
import io.quarkus.annotation.processor.documentation.config.model.ConfigPhase;
import io.quarkus.annotation.processor.documentation.config.model.ExtensionModule;
import io.quarkus.annotation.processor.documentation.config.util.Types;
import io.quarkus.annotation.processor.util.Config;
import io.quarkus.annotation.processor.util.Utils;
Expand Down Expand Up @@ -66,6 +68,16 @@ public void onResolvedEnum(TypeElement enumTypeElement) {
configCollector.addResolvedEnum(enumDefinition);
}

protected void validateRuntimeConfigOnDeploymentModules(ConfigPhase configPhase) {
if (configPhase.equals(ConfigPhase.RUN_TIME) || configPhase.equals(ConfigPhase.BUILD_AND_RUN_TIME_FIXED)) {
ExtensionModule.ExtensionModuleType type = config.getExtensionModule().type();
if (type.equals(ExtensionModule.ExtensionModuleType.DEPLOYMENT)) {
throw new IllegalStateException("Configuration classes with ConfigPhase.RUN_TIME or " +
"ConfigPhase.BUILD_AND_RUNTIME_FIXED phases, must be on runtime module.");
}
}
}

protected void handleCommonPropertyAnnotations(DiscoveryConfigProperty.Builder builder,
Map<String, AnnotationMirror> propertyAnnotations, ResolvedType resolvedType, String sourceElementName) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public Optional<DiscoveryConfigRoot> onConfigRoot(TypeElement configRoot) {
}
}

validateRuntimeConfigOnDeploymentModules(
configPhase);

for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : configMappingAnnotion.getElementValues()
.entrySet()) {
if ("prefix()".equals(entry.getKey().toString())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public Optional<DiscoveryConfigRoot> onConfigRoot(TypeElement configRoot) {
}
}

validateRuntimeConfigOnDeploymentModules(configPhase);

String overriddenDocPrefix = null;
if (configDocPrefixAnnotation != null) {
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : configDocPrefixAnnotation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.quarkus.annotation.processor.documentation.config.scanner;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import io.quarkus.annotation.processor.documentation.config.model.ConfigPhase;
import io.quarkus.annotation.processor.documentation.config.model.Extension;
import io.quarkus.annotation.processor.documentation.config.model.ExtensionModule;
import io.quarkus.annotation.processor.util.Config;
import io.quarkus.annotation.processor.util.Utils;

class AbstractConfigListenerTest {

@ParameterizedTest
@EnumSource(value = ConfigPhase.class, names = { "RUN_TIME", "BUILD_AND_RUN_TIME_FIXED" })
void shouldThrowExceptionWhenConfigIsRuntimeAndModuleTypeIsDeployment(ConfigPhase phase) {
Config config = new Config(
new ExtensionModule(
"io.quarkus",
"quarkus-extension-deployment",
ExtensionModule.ExtensionModuleType.DEPLOYMENT,
new Extension(
"io.quarkus",
"quarkus-extension-deployment",
"extension",
null,
false,
null,
true),
true),
false,
false);

AbstractConfigListener listener = new LegacyConfigRootListener(
config, new Utils(
null),
new ConfigCollector());

Assertions.assertThrows(IllegalStateException.class, () -> {
listener.validateRuntimeConfigOnDeploymentModules(phase);
});
}

@ParameterizedTest
@EnumSource(value = ConfigPhase.class, names = { "RUN_TIME", "BUILD_AND_RUN_TIME_FIXED" })
void shouldNotThrowExceptionWhenConfigIsRuntimeAndModuleTypeIsRunTime(ConfigPhase phase) {
Config config = new Config(
new ExtensionModule(
"io.quarkus",
"quarkus-extension",
ExtensionModule.ExtensionModuleType.RUNTIME,
new Extension(
"io.quarkus",
"quarkus-extension",
"extension",
Extension.NameSource.EXTENSION_METADATA,
false,
"https://quarkus.io/guide/quarkus-extension",
true),
true),
false,
false);

AbstractConfigListener listener = new LegacyConfigRootListener(
config, new Utils(
null),
new ConfigCollector());

Assertions.assertDoesNotThrow(() -> {
listener.validateRuntimeConfigOnDeploymentModules(phase);
});
}
}

0 comments on commit 71047bd

Please sign in to comment.