Skip to content
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

Add @ConfigMapping validateUnknown to enable / disable validation of unknown configurations in the mapping path #1238

Closed
wants to merge 1 commit into from
Closed
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 @@ -101,8 +101,7 @@ protected void processConfigProperties(

// Each config class is both in SmallRyeConfig and managed by a configurator bean.
// CDI requires more beans for injection points due to binding prefix.
ConfigClass properties = ConfigClass.configClass(annotatedType.getJavaClass(),
annotatedType.getAnnotation(ConfigProperties.class).prefix());
ConfigClass properties = ConfigClass.configClass(annotatedType.getJavaClass());
configProperties.add(properties);
configPropertiesBeans.add(properties);
}
Expand Down Expand Up @@ -132,7 +131,7 @@ protected void processConfigInjectionPoints(@Observes ProcessInjectionPoint<?, ?

if (injectionPoint.getAnnotated().isAnnotationPresent(ConfigProperties.class)) {
ConfigClass properties = ConfigClass.configClass((Class<?>) injectionPoint.getType(),
injectionPoint.getAnnotated().getAnnotation(ConfigProperties.class).prefix());
injectionPoint.getAnnotated().getAnnotation(ConfigProperties.class).prefix(), false);

// If the prefix is empty at the injection point, fallbacks to the class prefix (already registered)
if (!properties.getPrefix().equals(ConfigProperties.UNCONFIGURED_PREFIX)) {
Expand All @@ -145,7 +144,7 @@ protected void processConfigInjectionPoints(@Observes ProcessInjectionPoint<?, ?
// Add to SmallRyeConfig config classes with a different prefix by injection point
if (injectionPoint.getAnnotated().isAnnotationPresent(ConfigMapping.class)) {
ConfigClass mapping = ConfigClass.configClass((Class<?>) injectionPoint.getType(),
injectionPoint.getAnnotated().getAnnotation(ConfigMapping.class).prefix());
injectionPoint.getAnnotated().getAnnotation(ConfigMapping.class).prefix(), true);
// If the prefix is empty at the injection point, fallbacks to the class prefix (already registered)
if (!mapping.getPrefix().isEmpty()) {
configMappings.add(mapping);
Expand Down
11 changes: 11 additions & 0 deletions implementation/src/main/java/io/smallrye/config/ConfigMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
* }
* </pre>
*
* A Config Mapping must match every configuration path available in the Config system. If a property name
* <code>server.name</code> exists, then it must be mapped with a method <code>String name()</code>.
* <p>
* This annotation is also used in CDI aware environments to scan and register Config Mappings. Otherwise, Config
* Mapping interfaces require registration via
* {@link SmallRyeConfigBuilder#withMapping(java.lang.Class)}.
Expand All @@ -52,6 +55,14 @@
*/
NamingStrategy namingStrategy() default NamingStrategy.KEBAB_CASE;

/**
* Enable or disable the Config Mapping requirement to match every configuration path available in the Config
* system. By default, the validation is <b>enabled</b>.
*
* @return a boolean <code>true</code> to enable the validation, or <code>false</code> to disable it.
*/
boolean validateUnknown() default true;

enum NamingStrategy {
/**
* The method name is used as is to map the configuration property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ static String prefix(final String prefix, final String path) {
public static final class ConfigClass {
private final Class<?> klass;
private final String prefix;
private final boolean validateUnknown;

public ConfigClass(final Class<?> klass, final String prefix) {
public ConfigClass(final Class<?> klass, final String prefix, final boolean validateUnknown) {
this.klass = klass;
this.prefix = prefix;
this.validateUnknown = validateUnknown;
}

public Class<?> getKlass() {
Expand All @@ -133,6 +135,10 @@ public String getPrefix() {
return prefix;
}

public boolean isValidateUnknown() {
return validateUnknown;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand All @@ -151,21 +157,26 @@ public int hashCode() {
}

public static ConfigClass configClass(final Class<?> klass, final String prefix) {
return new ConfigClass(klass, prefix);
return new ConfigClass(klass, prefix, true);
}

public static ConfigClass configClass(final Class<?> klass, final String prefix, final boolean validateUnknown) {
return new ConfigClass(klass, prefix, validateUnknown);
}

public static ConfigClass configClass(final Class<?> klass) {
if (klass.isInterface()) {
ConfigMapping configMapping = klass.getAnnotation(ConfigMapping.class);
String prefix = configMapping != null ? configMapping.prefix() : "";
return configClass(klass, prefix);
boolean validateUnknown = configMapping == null || configMapping.validateUnknown();
return configClass(klass, prefix, validateUnknown);
} else {
ConfigProperties configProperties = klass.getAnnotation(ConfigProperties.class);
String prefix = configProperties != null ? configProperties.prefix() : "";
if (prefix.equals(ConfigProperties.UNCONFIGURED_PREFIX)) {
prefix = "";
}
return configClass(klass, prefix);
return configClass(klass, prefix, false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,11 @@ public final class MappingBuilder {
private final List<String> ignoredPaths = new ArrayList<>();

public void mapping(ConfigClass configClass) {
Assert.checkNotNullParam("configClass", configClass);
mapping(configClass.getKlass(), configClass.getPrefix());
if (!configClass.isValidateUnknown()) {
ignoredPath(configClass.getPrefix() + ".**");
}
}

public void mapping(Class<?> type, String prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2659,4 +2659,20 @@ interface Nested {
String value();
}
}

@Test
void ignoreUnknownAnnotation() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config("ignore.value", "value", "ignore.unknown", "unknown"))
.withMapping(IgnoreUnknownAnnotation.class)
.build();

IgnoreUnknownAnnotation mapping = config.getConfigMapping(IgnoreUnknownAnnotation.class);
assertEquals("value", mapping.value());
}

@ConfigMapping(prefix = "ignore", validateUnknown = false)
interface IgnoreUnknownAnnotation {
String value();
}
}