Browse Source

Polish "Resolved nested placeholder for CharSequence"

See gh-32876
pull/33258/head
Stéphane Nicoll 2 years ago
parent
commit
b6fbbeccdc
  1. 8
      spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java
  2. 25
      spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -84,8 +84,10 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
} }
Object value = propertySource.getProperty(key); Object value = propertySource.getProperty(key);
if (value != null) { if (value != null) {
if (resolveNestedPlaceholders && value instanceof String string) { if (resolveNestedPlaceholders &&
value = resolveNestedPlaceholders(string); (String.class.equals(targetValueType) || CharSequence.class.equals(targetValueType)) &&
value instanceof CharSequence cs) {
value = resolveNestedPlaceholders(cs.toString());
} }
logKeyFound(key, propertySource, value); logKeyFound(key, propertySource, value);
return convertValueIfNecessary(value, targetValueType); return convertValueIfNecessary(value, targetValueType);

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

@ -300,6 +300,31 @@ class PropertySourcesPropertyResolverTests {
.withMessageContaining("Circular"); .withMessageContaining("Circular");
} }
@Test
void resolveNestedPlaceholdersIfValueIsCharSequence() {
MutablePropertySources ps = new MutablePropertySources();
ps.addFirst(new MockPropertySource()
.withProperty("p1", "v1")
.withProperty("p2", "v2")
.withProperty("p3", new StringBuilder("${p1}:${p2}")));
ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps);
assertThat(pr.getProperty("p1")).isEqualTo("v1");
assertThat(pr.getProperty("p2")).isEqualTo("v2");
assertThat(pr.getProperty("p3")).isEqualTo("v1:v2");
}
@Test
void resolveNestedPlaceholdersIfValueIsCharSequenceAndStringBuilderIsRequested() {
MutablePropertySources ps = new MutablePropertySources();
ps.addFirst(new MockPropertySource()
.withProperty("p1", "v1")
.withProperty("p2", "v2")
.withProperty("p3", new StringBuilder("${p1}:${p2}")));
ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps);
assertThat(pr.getProperty("p3", StringBuilder.class)).isInstanceOf(StringBuilder.class)
.hasToString("${p1}:${p2}");
}
@Test @Test
void ignoreUnresolvableNestedPlaceholdersIsConfigurable() { void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
MutablePropertySources ps = new MutablePropertySources(); MutablePropertySources ps = new MutablePropertySources();

Loading…
Cancel
Save