Skip to content

Commit

Permalink
Filter environment variables to be mapped with mappings roots
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Aug 28, 2023
1 parent 372a9d3 commit 84164e9
Showing 1 changed file with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,6 @@ ConfigMappingContext mapConfiguration(SmallRyeConfig config) throws ConfigValida
defaultValuesConfigSource.registerDefaults(defaultValues);
}
}

config.addPropertyNames(additionalMappedProperties(new HashSet<>(getProperties().keySet()), config));
return SecretKeys.doUnlocked(() -> mapConfigurationInternal(config));
}

Expand All @@ -982,6 +980,8 @@ private ConfigMappingContext mapConfigurationInternal(SmallRyeConfig config) thr
}
}

config.addPropertyNames(additionalMappedProperties(new HashSet<>(getProperties().keySet()), roots.keySet(), config));

// lazily sweep
for (String name : config.getPropertyNames()) {
NameIterator ni = new NameIterator(name);
Expand Down Expand Up @@ -1053,7 +1053,11 @@ private boolean isPropertyInRoot(NameIterator propertyName) {
return false;
}

private static Set<String> additionalMappedProperties(final Set<String> mappedProperties, final SmallRyeConfig config) {
private static Set<String> additionalMappedProperties(
final Set<String> mappedProperties,
final Set<String> roots,
final SmallRyeConfig config) {

// Collect EnvSource properties
Set<String> envProperties = new HashSet<>();
for (ConfigSource source : config.getConfigSources(EnvConfigSource.class)) {
Expand All @@ -1065,6 +1069,32 @@ private static Set<String> additionalMappedProperties(final Set<String> mappedPr
mappedProperties.remove(propertyName);
}

Set<String> envRoots = new HashSet<>();
for (String root : roots) {
envRoots.add(replaceNonAlphanumericByUnderscores(root));
}

// Ignore Env properties that don't belong to a root
Set<String> envPropertiesUnmapped = new HashSet<>();
for (String envProperty : envProperties) {
boolean matched = false;
for (String envRoot : envRoots) {
if (envProperty.length() < envRoot.length()) {
continue;
}

if (envRoot.equalsIgnoreCase(envProperty.substring(0, envRoot.length()))) {
matched = true;
break;
}
}

if (!matched) {
envPropertiesUnmapped.add(envProperty);
}
}
envProperties.removeAll(envPropertiesUnmapped);

Set<String> additionalMappedProperties = new HashSet<>();
// Look for unmatched properties if we can find one in the Env ones and add it
NameIterator nameIterator = NameIterator.empty();
Expand Down

0 comments on commit 84164e9

Please sign in to comment.