Skip to content

Commit 5a7bfa9

Browse files
quaffsnicoll
authored andcommitted
Extend nested placeholders resolution to any CharSequence
See gh-32876
1 parent 181b680 commit 5a7bfa9

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-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.
@@ -24,6 +24,7 @@
2424
*
2525
* @author Chris Beams
2626
* @author Juergen Hoeller
27+
* @author Yanming Zhou
2728
* @since 3.1
2829
* @see PropertySource
2930
* @see PropertySources
@@ -84,8 +85,8 @@ protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolv
8485
}
8586
Object value = propertySource.getProperty(key);
8687
if (value != null) {
87-
if (resolveNestedPlaceholders && value instanceof String string) {
88-
value = resolveNestedPlaceholders(string);
88+
if (resolveNestedPlaceholders && value instanceof CharSequence cs) {
89+
value = resolveNestedPlaceholders(cs.toString());
8990
}
9091
logKeyFound(key, propertySource, value);
9192
return convertValueIfNecessary(value, targetValueType);

spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
/**
3636
* @author Chris Beams
37+
* @author Yanming Zhou
3738
* @since 3.1
3839
*/
3940
class PropertySourcesPropertyResolverTests {
@@ -300,6 +301,43 @@ void resolveNestedPropertyPlaceholders() {
300301
.withMessageContaining("Circular");
301302
}
302303

304+
@Test
305+
void resolveNestedPlaceholdersIfValueIsCharSequence() {
306+
MutablePropertySources ps = new MutablePropertySources();
307+
ps.addFirst(new MockPropertySource()
308+
.withProperty("p1", "v1")
309+
.withProperty("p2", "v2")
310+
.withProperty("p3", new CharSequence() {
311+
312+
static final String underlying = "${p1}:${p2}";
313+
314+
@Override
315+
public int length() {
316+
return underlying.length();
317+
}
318+
319+
@Override
320+
public char charAt(int index) {
321+
return underlying.charAt(index);
322+
}
323+
324+
@Override
325+
public CharSequence subSequence(int start, int end) {
326+
return underlying.subSequence(start, end);
327+
}
328+
329+
@Override
330+
public String toString() {
331+
return underlying;
332+
}
333+
})
334+
);
335+
ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps);
336+
assertThat(pr.getProperty("p1")).isEqualTo("v1");
337+
assertThat(pr.getProperty("p2")).isEqualTo("v2");
338+
assertThat(pr.getProperty("p3")).isEqualTo("v1:v2");
339+
}
340+
303341
@Test
304342
void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
305343
MutablePropertySources ps = new MutablePropertySources();

0 commit comments

Comments
 (0)