diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java index 83b02dcc00..a0cfbf3048 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java @@ -125,7 +125,7 @@ public final class RelaxedNames implements Iterable { return builder.toString(); } }, - UNDERSCORE_TO_CAMELCASE { + SEPARATED_TO_CAMELCASE { @Override public String apply(String value) { StringBuilder builder = new StringBuilder(); diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedPropertyResolver.java b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedPropertyResolver.java index d37a9e0277..6e672281e6 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedPropertyResolver.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedPropertyResolver.java @@ -81,9 +81,13 @@ public class RelaxedPropertyResolver implements PropertyResolver { @Override public T getProperty(String key, Class targetType, T defaultValue) { - for (String relaxedKey : new RelaxedNames(this.prefix + key)) { - if (this.resolver.containsProperty(relaxedKey)) { - return this.resolver.getProperty(relaxedKey, targetType, defaultValue); + RelaxedNames prefixes = new RelaxedNames(this.prefix); + RelaxedNames keys = new RelaxedNames(key); + for (String prefix : prefixes) { + for (String relaxedKey : keys) { + if (this.resolver.containsProperty(prefix + relaxedKey)) { + return this.resolver.getProperty(prefix + relaxedKey, targetType); + } } } return defaultValue; @@ -91,9 +95,14 @@ public class RelaxedPropertyResolver implements PropertyResolver { @Override public Class getPropertyAsClass(String key, Class targetType) { - for (String relaxedKey : new RelaxedNames(this.prefix + key)) { - if (this.resolver.containsProperty(relaxedKey)) { - return this.resolver.getPropertyAsClass(relaxedKey, targetType); + RelaxedNames prefixes = new RelaxedNames(this.prefix); + RelaxedNames keys = new RelaxedNames(key); + for (String prefix : prefixes) { + for (String relaxedKey : keys) { + if (this.resolver.containsProperty(prefix + relaxedKey)) { + return this.resolver.getPropertyAsClass(prefix + relaxedKey, + targetType); + } } } return null; @@ -101,9 +110,13 @@ public class RelaxedPropertyResolver implements PropertyResolver { @Override public boolean containsProperty(String key) { - for (String relaxedKey : new RelaxedNames(this.prefix + key)) { - if (this.resolver.containsProperty(relaxedKey)) { - return true; + RelaxedNames prefixes = new RelaxedNames(this.prefix); + RelaxedNames keys = new RelaxedNames(key); + for (String prefix : prefixes) { + for (String relaxedKey : keys) { + if (this.resolver.containsProperty(prefix + relaxedKey)) { + return true; + } } } return false; @@ -128,15 +141,14 @@ public class RelaxedPropertyResolver implements PropertyResolver { * {@link ConfigurableEnvironment}. * @param keyPrefix the key prefix used to filter results * @return a map of all sub properties starting with the specified key prefix. - * @see #getSubProperties(PropertySources, RelaxedNames) - * @see #getSubProperties(PropertySources, String, RelaxedNames) + * @see #getSubProperties(PropertySources, String) + * @see #getSubProperties(PropertySources, String, String) */ public Map getSubProperties(String keyPrefix) { Assert.isInstanceOf(ConfigurableEnvironment.class, this.resolver, "SubProperties not available."); ConfigurableEnvironment env = (ConfigurableEnvironment) this.resolver; - return getSubProperties(env.getPropertySources(), this.prefix, new RelaxedNames( - keyPrefix)); + return getSubProperties(env.getPropertySources(), this.prefix, keyPrefix); } /** @@ -145,10 +157,10 @@ public class RelaxedPropertyResolver implements PropertyResolver { * @param propertySources the property sources to scan * @param keyPrefix the key prefixes to test * @return a map of all sub properties starting with the specified key prefixes. - * @see #getSubProperties(PropertySources, String, RelaxedNames) + * @see #getSubProperties(PropertySources, String, String) */ public static Map getSubProperties(PropertySources propertySources, - RelaxedNames keyPrefix) { + String keyPrefix) { return getSubProperties(propertySources, null, keyPrefix); } @@ -160,16 +172,17 @@ public class RelaxedPropertyResolver implements PropertyResolver { * {@code null}) * @param keyPrefix the key prefixes to test * @return a map of all sub properties starting with the specified key prefixes. - * @see #getSubProperties(PropertySources, String, RelaxedNames) + * @see #getSubProperties(PropertySources, String, String) */ public static Map getSubProperties(PropertySources propertySources, - String rootPrefix, RelaxedNames keyPrefix) { + String rootPrefix, String keyPrefix) { + RelaxedNames keyPrefixes = new RelaxedNames(keyPrefix); Map subProperties = new LinkedHashMap(); for (PropertySource source : propertySources) { if (source instanceof EnumerablePropertySource) { for (String name : ((EnumerablePropertySource) source) .getPropertyNames()) { - String key = getSubKey(name, rootPrefix, keyPrefix); + String key = getSubKey(name, rootPrefix, keyPrefixes); if (key != null) { subProperties.put(key, source.getProperty(name)); } @@ -179,11 +192,14 @@ public class RelaxedPropertyResolver implements PropertyResolver { return Collections.unmodifiableMap(subProperties); } - private static String getSubKey(String name, String rootPrefix, RelaxedNames keyPrefix) { - rootPrefix = (rootPrefix == null ? "" : rootPrefix); - for (String candidateKeyPrefix : keyPrefix) { - if (name.startsWith(rootPrefix + candidateKeyPrefix)) { - return name.substring((rootPrefix + candidateKeyPrefix).length()); + private static String getSubKey(String name, String rootPrefixes, + RelaxedNames keyPrefix) { + rootPrefixes = (rootPrefixes == null ? "" : rootPrefixes); + for (String rootPrefix : new RelaxedNames(rootPrefixes)) { + for (String candidateKeyPrefix : keyPrefix) { + if (name.startsWith(rootPrefix + candidateKeyPrefix)) { + return name.substring((rootPrefix + candidateKeyPrefix).length()); + } } } return null; diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedPropertyResolverTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedPropertyResolverTests.java index c9ec646a11..f1a9d72443 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedPropertyResolverTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedPropertyResolverTests.java @@ -51,6 +51,7 @@ public class RelaxedPropertyResolverTests { this.environment = new StandardEnvironment(); this.source = new LinkedHashMap(); this.source.put("myString", "value"); + this.source.put("myobject", "object"); this.source.put("myInteger", 123); this.source.put("myClass", "java.lang.String"); this.environment.getPropertySources().addFirst( @@ -88,6 +89,12 @@ public class RelaxedPropertyResolverTests { assertThat(this.resolver.getProperty("my-missing"), nullValue()); } + @Test + public void getPropertyNoSeparator() throws Exception { + assertThat(this.resolver.getProperty("myobject"), equalTo("object")); + assertThat(this.resolver.getProperty("my-object"), equalTo("object")); + } + @Test public void getPropertyWithDefault() throws Exception { assertThat(this.resolver.getProperty("my-string", "a"), equalTo("value")); @@ -148,8 +155,10 @@ public class RelaxedPropertyResolverTests { public void prefixedRelaxed() throws Exception { this.resolver = new RelaxedPropertyResolver(this.environment, "a."); this.source.put("A_B", "test"); + this.source.put("a.foobar", "spam"); assertThat(this.resolver.containsProperty("b"), equalTo(true)); assertThat(this.resolver.getProperty("b"), equalTo("test")); + assertThat(this.resolver.getProperty("foo-bar"), equalTo("spam")); } @Test