From 84164e99af0acc703b9809e08993d288ca6314c1 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Mon, 28 Aug 2023 15:17:06 +0100 Subject: [PATCH] Filter environment variables to be mapped with mappings roots --- .../config/ConfigMappingProvider.java | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java b/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java index 8eeeb02f6..cfd8f3120 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java @@ -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)); } @@ -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); @@ -1053,7 +1053,11 @@ private boolean isPropertyInRoot(NameIterator propertyName) { return false; } - private static Set additionalMappedProperties(final Set mappedProperties, final SmallRyeConfig config) { + private static Set additionalMappedProperties( + final Set mappedProperties, + final Set roots, + final SmallRyeConfig config) { + // Collect EnvSource properties Set envProperties = new HashSet<>(); for (ConfigSource source : config.getConfigSources(EnvConfigSource.class)) { @@ -1065,6 +1069,32 @@ private static Set additionalMappedProperties(final Set mappedPr mappedProperties.remove(propertyName); } + Set envRoots = new HashSet<>(); + for (String root : roots) { + envRoots.add(replaceNonAlphanumericByUnderscores(root)); + } + + // Ignore Env properties that don't belong to a root + Set 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 additionalMappedProperties = new HashSet<>(); // Look for unmatched properties if we can find one in the Env ones and add it NameIterator nameIterator = NameIterator.empty();