Skip to content

Commit cda538a

Browse files
committed
Resolve nested placeholders if value is CharSequence
Fix GH-34195
1 parent 33057d3 commit cda538a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesPropertyResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* underlying sources if the name is a value {@link ConfigurationPropertyName}.
2828
*
2929
* @author Phillip Webb
30+
* @author Yanming Zhou
3031
*/
3132
class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResolver {
3233

@@ -76,8 +77,8 @@ private <T> T getProperty(String key, Class<T> targetValueType, boolean resolveN
7677
if (value == null) {
7778
return null;
7879
}
79-
if (resolveNestedPlaceholders && value instanceof String string) {
80-
value = resolveNestedPlaceholders(string);
80+
if (resolveNestedPlaceholders && value instanceof CharSequence cs) {
81+
value = resolveNestedPlaceholders(cs.toString());
8182
}
8283
return convertValueIfNecessary(value, targetValueType);
8384
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesPropertyResolverTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* Tests for {@link ConfigurationPropertySourcesPropertyResolver}.
3434
*
3535
* @author Phillip Webb
36+
* @author Yanming Zhou
3637
*/
3738
class ConfigurationPropertySourcesPropertyResolverTests {
3839

@@ -113,6 +114,40 @@ void getPropertyAsTypeWhenHasPlaceholder() {
113114
assertThat(environment.getProperty("v2", Integer.class)).isOne();
114115
}
115116

117+
@Test // gh-34195
118+
void resolveNestedPlaceholdersIfValueIsCharSequence() {
119+
ResolverEnvironment environment = new ResolverEnvironment();
120+
MockPropertySource propertySource = new MockPropertySource();
121+
propertySource.withProperty("v1", "1");
122+
propertySource.withProperty("v2", new CharSequence() {
123+
124+
static final String underlying = "${v1}";
125+
126+
@Override
127+
public int length() {
128+
return underlying.length();
129+
}
130+
131+
@Override
132+
public char charAt(int index) {
133+
return underlying.charAt(index);
134+
}
135+
136+
@Override
137+
public CharSequence subSequence(int start, int end) {
138+
return underlying.subSequence(start, end);
139+
}
140+
141+
@Override
142+
public String toString() {
143+
return underlying;
144+
}
145+
});
146+
environment.getPropertySources().addFirst(propertySource);
147+
assertThat(environment.getProperty("v2")).isEqualTo("1");
148+
assertThat(environment.getProperty("v2", Integer.class)).isOne();
149+
}
150+
116151
private CountingMockPropertySource createMockPropertySource(StandardEnvironment environment, boolean attach) {
117152
CountingMockPropertySource propertySource = new CountingMockPropertySource();
118153
propertySource.withProperty("spring", "boot");

0 commit comments

Comments
 (0)