Skip to content

Commit

Permalink
Resolve property placeholders when binding properties to a Map
Browse files Browse the repository at this point in the history
Add call to placeholder resolver to ensure property placeholders
are resolved for the `MapBinder`

See gh-39507

Signed-off-by: wanger26 <jakobwanger@gmail.com>
  • Loading branch information
wanger26 authored and scottfrederick committed Feb 11, 2024
1 parent 7b5725f commit 8292104
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> targe
ConfigurationProperty property = source.getConfigurationProperty(name);
if (property != null && !hasDescendants) {
getContext().setConfigurationProperty(property);
return getContext().getConverter().convert(property.getValue(), target);
Object result = property.getValue();
result = getContext().getPlaceholdersResolver().resolvePlaceholders(result);
result = getContext().getConverter().convert(result, target);
return result;
}
source = source.filter(name::isAncestorOf);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,19 @@ void bindToMapWithWildcardShouldConvertToTheRightType() {
.containsExactly("127.0.0.1", "127.0.0.2");
}

@Test
void bindToMapWithPlaceholdersShouldResolve() {
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new MapConverter());
StandardEnvironment environment = new StandardEnvironment();
Binder binder = new Binder(this.sources, new PropertySourcesPlaceholdersResolver(environment), conversionService, null, null);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, "bar=bc");
this.sources.add(new MockConfigurationPropertySource("foo", "a${bar},${bar}d"));
Map<String, String> map = binder.bind("foo", STRING_STRING_MAP).get();
assertThat(map).containsKey("abc");
assertThat(map).containsKey("bcd");
}

private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric, ResolvableType valueType) {
ResolvableType keyType = ResolvableType.forClass(keyGeneric);
return Bindable.of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType));
Expand Down

0 comments on commit 8292104

Please sign in to comment.