Skip to content

Commit e8db492

Browse files
committed
Resolve nested placeholders as possible if value is CharSequence
Fix GH-34195
1 parent fa131fa commit e8db492

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

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

Lines changed: 11 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,16 @@ 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) {
81+
if (value instanceof String string) {
82+
value = resolveNestedPlaceholders(string);
83+
}
84+
else if (value instanceof CharSequence cs && value.getClass() != targetValueType) {
85+
// keep value as it is if targetValueType is equal to value's type
86+
// to avoid potential ConverterNotFoundException while converting String
87+
// back to value's type
88+
value = resolveNestedPlaceholders(cs.toString());
89+
}
8190
}
8291
return convertValueIfNecessary(value, targetValueType);
8392
}

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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,45 @@ void getPropertyAsTypeWhenHasPlaceholder() {
113114
assertThat(environment.getProperty("v2", Integer.class)).isOne();
114115
}
115116

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

0 commit comments

Comments
 (0)