Skip to content

Commit 8292104

Browse files
wanger26scottfrederick
authored andcommitted
Resolve property placeholders when binding properties to a Map
Add call to placeholder resolver to ensure property placeholders are resolved for the `MapBinder` See gh-39507 Signed-off-by: wanger26 <[email protected]>
1 parent 7b5725f commit 8292104

File tree

2 files changed

+17
-1
lines changed
  • spring-boot-project/spring-boot/src

2 files changed

+17
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> targe
6262
ConfigurationProperty property = source.getConfigurationProperty(name);
6363
if (property != null && !hasDescendants) {
6464
getContext().setConfigurationProperty(property);
65-
return getContext().getConverter().convert(property.getValue(), target);
65+
Object result = property.getValue();
66+
result = getContext().getPlaceholdersResolver().resolvePlaceholders(result);
67+
result = getContext().getConverter().convert(result, target);
68+
return result;
6669
}
6770
source = source.filter(name::isAncestorOf);
6871
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,19 @@ void bindToMapWithWildcardShouldConvertToTheRightType() {
610610
.containsExactly("127.0.0.1", "127.0.0.2");
611611
}
612612

613+
@Test
614+
void bindToMapWithPlaceholdersShouldResolve() {
615+
DefaultConversionService conversionService = new DefaultConversionService();
616+
conversionService.addConverter(new MapConverter());
617+
StandardEnvironment environment = new StandardEnvironment();
618+
Binder binder = new Binder(this.sources, new PropertySourcesPlaceholdersResolver(environment), conversionService, null, null);
619+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, "bar=bc");
620+
this.sources.add(new MockConfigurationPropertySource("foo", "a${bar},${bar}d"));
621+
Map<String, String> map = binder.bind("foo", STRING_STRING_MAP).get();
622+
assertThat(map).containsKey("abc");
623+
assertThat(map).containsKey("bcd");
624+
}
625+
613626
private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric, ResolvableType valueType) {
614627
ResolvableType keyType = ResolvableType.forClass(keyGeneric);
615628
return Bindable.of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType));

0 commit comments

Comments
 (0)