From bdf76b2f8d80d590f42f8f8a0616234a202761cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Thu, 17 Oct 2024 10:22:08 +0200 Subject: [PATCH] Restore nested property resolution for non CharSequence types Closes gh-33727 Co-authored-by: Andy Wilkinson --- .../core/env/PropertySourcesPropertyResolver.java | 12 ++++++++---- .../env/PropertySourcesPropertyResolverTests.java | 8 ++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java b/spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java index a9c071781f..3558d0fa77 100644 --- a/spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java +++ b/spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java @@ -84,10 +84,14 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver { } Object value = propertySource.getProperty(key); if (value != null) { - if (resolveNestedPlaceholders && - (String.class.equals(targetValueType) || CharSequence.class.equals(targetValueType)) && - value instanceof CharSequence cs) { - value = resolveNestedPlaceholders(cs.toString()); + if (resolveNestedPlaceholders) { + if (value instanceof String string) { + value = resolveNestedPlaceholders(string); + } + else if ((value instanceof CharSequence cs) && (String.class.equals(targetValueType) || + CharSequence.class.equals(targetValueType))) { + value = resolveNestedPlaceholders(cs.toString()); + } } logKeyFound(key, propertySource, value); return convertValueIfNecessary(value, targetValueType); diff --git a/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java b/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java index 16d8d47572..23654dfe10 100644 --- a/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java @@ -325,6 +325,14 @@ class PropertySourcesPropertyResolverTests { .hasToString("${p1}:${p2}"); } + @Test // gh-33727 + void resolveNestedPlaceHolderIfValueShouldConvertToOtherTypes() { + MutablePropertySources ps = new MutablePropertySources(); + ps.addFirst(new MockPropertySource().withProperty("new.enabled", "${old.enabled:true}")); + ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps); + assertThat(pr.getProperty("new.enabled", Boolean.class, false)).isTrue(); + } + @Test void ignoreUnresolvableNestedPlaceholdersIsConfigurable() { MutablePropertySources ps = new MutablePropertySources();