Skip to content

Commit 3b2a4da

Browse files
committed
Merge pull request #32876 from quaff
* pr/32876: Polish "Extend nested placeholders resolution to any CharSequence" Extend nested placeholders resolution to any CharSequence Closes gh-32876
2 parents 181b680 + 79b5ee7 commit 3b2a4da

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

+3-3
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,8 @@ 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 && value instanceof CharSequence cs) {
88+
value = resolveNestedPlaceholders(cs.toString());
8989
}
9090
logKeyFound(key, propertySource, value);
9191
return convertValueIfNecessary(value, targetValueType);

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

+13
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,19 @@ 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+
303316
@Test
304317
void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
305318
MutablePropertySources ps = new MutablePropertySources();

0 commit comments

Comments
 (0)