Skip to content

Commit 0030144

Browse files
committed
Merge pull request #32876 from quaff
* pr/32876: Polish "Resolved nested placeholder for CharSequence" Closes gh-32876
2 parents 5331499 + b6fbbec commit 0030144

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 5 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.
@@ -84,8 +84,10 @@ protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolv
8484
}
8585
Object value = propertySource.getProperty(key);
8686
if (value != null) {
87-
if (resolveNestedPlaceholders && value instanceof String string) {
88-
value = resolveNestedPlaceholders(string);
87+
if (resolveNestedPlaceholders &&
88+
(String.class.equals(targetValueType) || CharSequence.class.equals(targetValueType)) &&
89+
value instanceof CharSequence cs) {
90+
value = resolveNestedPlaceholders(cs.toString());
8991
}
9092
logKeyFound(key, propertySource, value);
9193
return convertValueIfNecessary(value, targetValueType);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,31 @@ void resolveNestedPropertyPlaceholders() {
300300
.withMessageContaining("Circular");
301301
}
302302

303+
@Test
304+
void resolveNestedPlaceholdersIfValueIsCharSequence() {
305+
MutablePropertySources ps = new MutablePropertySources();
306+
ps.addFirst(new MockPropertySource()
307+
.withProperty("p1", "v1")
308+
.withProperty("p2", "v2")
309+
.withProperty("p3", new StringBuilder("${p1}:${p2}")));
310+
ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps);
311+
assertThat(pr.getProperty("p1")).isEqualTo("v1");
312+
assertThat(pr.getProperty("p2")).isEqualTo("v2");
313+
assertThat(pr.getProperty("p3")).isEqualTo("v1:v2");
314+
}
315+
316+
@Test
317+
void resolveNestedPlaceholdersIfValueIsCharSequenceAndStringBuilderIsRequested() {
318+
MutablePropertySources ps = new MutablePropertySources();
319+
ps.addFirst(new MockPropertySource()
320+
.withProperty("p1", "v1")
321+
.withProperty("p2", "v2")
322+
.withProperty("p3", new StringBuilder("${p1}:${p2}")));
323+
ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps);
324+
assertThat(pr.getProperty("p3", StringBuilder.class)).isInstanceOf(StringBuilder.class)
325+
.hasToString("${p1}:${p2}");
326+
}
327+
303328
@Test
304329
void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
305330
MutablePropertySources ps = new MutablePropertySources();

0 commit comments

Comments
 (0)