Skip to content

Commit

Permalink
Add ability to disable normalization for property keys
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 committed Oct 3, 2024
1 parent eccab50 commit 115d970
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package io.micronaut.inject.configproperties.eachbeanparameter
import io.micronaut.annotation.processing.test.AbstractTypeElementSpec
import io.micronaut.context.ApplicationContext
import io.micronaut.context.Qualifier
import io.micronaut.context.env.PropertySource
import io.micronaut.context.env.SystemPropertiesPropertySource
import io.micronaut.context.exceptions.NonUniqueBeanException
import io.micronaut.inject.qualifiers.PrimaryQualifier
import io.micronaut.inject.qualifiers.Qualifiers
Expand Down Expand Up @@ -59,4 +61,27 @@ class EachBeanParameterSpec extends AbstractTypeElementSpec {
cleanup:
ctx.close()
}

void 'test disabled normalization property keys'() {
when:
Map<String, Object> datasourcesConfiguration = [
'app.tables.Table-1.url': 'url1',
'app.tables.Table-2.url': 'url2',
]
ApplicationContext ctx = ApplicationContext.builder()
.propertySources(PropertySource.of(PropertySource.CONTEXT, ['spec': 'DisabledNormalizationSpec'] + datasourcesConfiguration, SystemPropertiesPropertySource.POSITION + 100))
.normalizePropertyKeys(false)
.start()

then:
ctx

def tablePropsBeans = ctx.getBeansOfType(TableProps)
tablePropsBeans.size() == 2
ctx.getBean(TableProps, Qualifiers.byName("Table-1")).name == "Table-1"
ctx.getBean(TableProps, Qualifiers.byName("Table-2")).name == "Table-2"

cleanup:
ctx.close()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.micronaut.inject.configproperties.eachbeanparameter;

import io.micronaut.context.annotation.ConfigurationInject;
import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
import io.micronaut.context.annotation.Requires;

@Requires(property = "spec", value = "DisabledNormalizationSpec")
@EachProperty("app.tables")
public class TableProps {

private String name;
private String url;
private int serverPort;

@ConfigurationInject
public TableProps(@Parameter String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getServerPort() {
return serverPort;
}

public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ public interface ApplicationContextBuilder {
*/
@NonNull ApplicationContextBuilder banner(boolean isEnabled);

/**
* Normalize configuration property keys or not. This flag used for bean names with {@code @EachProperty}
* and also {@link io.micronaut.context.annotation.Parameter} marked property values.
*
* @param normalizePropertyKeys Normalize configuration property keys or not
* @return This application
*/
@NonNull ApplicationContextBuilder normalizePropertyKeys(boolean normalizePropertyKeys);

/**
* Whether to error on an empty bean provider. Defaults to {@code false}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ default boolean isBannerEnabled() {
return true;
}

/**
* Normalization property keys enabled by default.
*
* @return Normalization property keys enabled by default.
*/
default boolean isNormalizePropertyKeys() {
return true;
}

@Nullable
@SuppressWarnings("java:S2447") // null used to establish absence of config
default Boolean isBootstrapEnvironmentEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class DefaultApplicationContextBuilder implements ApplicationContextBuild
private final Set<Class<? extends Annotation>> eagerInitAnnotated = new HashSet<>(3);
private String[] overrideConfigLocations;
private boolean banner = true;
private boolean normalizePropertyKeys = true;
private ClassPathResourceLoader classPathResourceLoader;
private boolean allowEmptyProviders = false;
private Boolean bootstrapEnvironment = null;
Expand Down Expand Up @@ -145,6 +146,11 @@ public boolean isBannerEnabled() {
return banner;
}

@Override
public boolean isNormalizePropertyKeys() {
return normalizePropertyKeys;
}

@Nullable
@Override
public Boolean isBootstrapEnvironmentEnabled() {
Expand Down Expand Up @@ -426,6 +432,12 @@ protected ApplicationContext newApplicationContext() {
return this;
}

@Override
public @NonNull ApplicationContextBuilder normalizePropertyKeys(boolean normalizePropertyKeys) {
this.normalizePropertyKeys = normalizePropertyKeys;
return this;
}

@Override
public @NonNull ApplicationContextBuilder allowEmptyProviders(boolean shouldAllow) {
this.allowEmptyProviders = shouldAllow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public DefaultEnvironment(@NonNull ApplicationContextConfiguration configuration
* @param logEnabled flag to enable or disable logger
*/
public DefaultEnvironment(@NonNull ApplicationContextConfiguration configuration, boolean logEnabled) {
super(configuration.getConversionService().orElseGet(MutableConversionService::create), logEnabled);
super(configuration.getConversionService().orElseGet(MutableConversionService::create), logEnabled, configuration.isNormalizePropertyKeys());
this.mutableConversionService = (MutableConversionService) conversionService;
this.configuration = configuration;
this.resourceLoader = configuration.getResourceLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,20 @@ public class PropertySourcePropertyResolver implements PropertyResolver, AutoClo
*/
private final Map<ConversionCacheKey, Object> resolvedValueCache = new ConcurrentHashMap<>(20);
private final EnvironmentProperties environmentProperties = EnvironmentProperties.fork(CURRENT_ENV);
private final boolean normalizePropertyKeys;

/**
* Creates a new, initially empty, {@link PropertySourcePropertyResolver} for the given {@link ConversionService}.
*
* @param conversionService The {@link ConversionService}
* @param logEnabled flag to enable or disable logger
* @param normalizePropertyKeys flag to enable or disable normalization property keys in getPropertyEntries
*/
public PropertySourcePropertyResolver(ConversionService conversionService, boolean logEnabled) {
public PropertySourcePropertyResolver(ConversionService conversionService, boolean logEnabled, boolean normalizePropertyKeys) {
this.log = logEnabled ? LoggerFactory.getLogger(getClass()) : NOPLogger.NOP_LOGGER;
this.conversionService = conversionService;
this.propertyPlaceholderResolver = new DefaultPropertyPlaceholderResolver(this, conversionService);
this.normalizePropertyKeys = normalizePropertyKeys;
}

/**
Expand All @@ -112,7 +115,7 @@ public PropertySourcePropertyResolver(ConversionService conversionService, boole
* @param conversionService The {@link ConversionService}
*/
public PropertySourcePropertyResolver(ConversionService conversionService) {
this(conversionService, true);
this(conversionService, true, true);
}

/**
Expand Down Expand Up @@ -216,7 +219,7 @@ public Collection<String> getPropertyEntries(@NonNull String name) {
if (StringUtils.isEmpty(name)) {
return Collections.emptySet();
}
Map<String, Object> entries = resolveEntriesForKey(name, false, PropertyCatalog.NORMALIZED);
Map<String, Object> entries = resolveEntriesForKey(name, false, normalizePropertyKeys ? PropertyCatalog.NORMALIZED : PropertyCatalog.RAW);
if (entries == null) {
return Collections.emptySet();
}
Expand Down

0 comments on commit 115d970

Please sign in to comment.