mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
Resolve nested placeholders as possible if value is CharSequence
Fix GH-34195
This commit is contained in:
parent
fa131fa6a0
commit
65f993f856
@ -27,6 +27,7 @@ import org.springframework.core.env.PropertySourcesPropertyResolver;
|
|||||||
* underlying sources if the name is a value {@link ConfigurationPropertyName}.
|
* underlying sources if the name is a value {@link ConfigurationPropertyName}.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Yanming Zhou
|
||||||
*/
|
*/
|
||||||
class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||||
|
|
||||||
@ -76,8 +77,16 @@ class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResol
|
|||||||
if (value == null) {
|
if (value == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (resolveNestedPlaceholders && value instanceof String string) {
|
if (resolveNestedPlaceholders) {
|
||||||
value = resolveNestedPlaceholders(string);
|
if (value instanceof String string) {
|
||||||
|
value = resolveNestedPlaceholders(string);
|
||||||
|
}
|
||||||
|
else if (value instanceof CharSequence cs && !targetValueType.isInstance(value)) {
|
||||||
|
// keep value as it is if value is instance of targetValueType
|
||||||
|
// to avoid potential ConverterNotFoundException while converting String
|
||||||
|
// back to value's type
|
||||||
|
value = resolveNestedPlaceholders(cs.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return convertValueIfNecessary(value, targetValueType);
|
return convertValueIfNecessary(value, targetValueType);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2023 the original author or authors.
|
* Copyright 2012-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.
|
||||||
@ -33,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
* Tests for {@link ConfigurationPropertySourcesPropertyResolver}.
|
* Tests for {@link ConfigurationPropertySourcesPropertyResolver}.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Yanming Zhou
|
||||||
*/
|
*/
|
||||||
class ConfigurationPropertySourcesPropertyResolverTests {
|
class ConfigurationPropertySourcesPropertyResolverTests {
|
||||||
|
|
||||||
@ -113,6 +114,45 @@ class ConfigurationPropertySourcesPropertyResolverTests {
|
|||||||
assertThat(environment.getProperty("v2", Integer.class)).isOne();
|
assertThat(environment.getProperty("v2", Integer.class)).isOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // gh-34195
|
||||||
|
void resolveNestedPlaceholdersIfValueIsCharSequence() {
|
||||||
|
CharSequence cs = new CharSequence() {
|
||||||
|
|
||||||
|
static final String underlying = "${v1}";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int length() {
|
||||||
|
return underlying.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char charAt(int index) {
|
||||||
|
return underlying.charAt(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CharSequence subSequence(int start, int end) {
|
||||||
|
return underlying.subSequence(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return underlying;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
ResolverEnvironment environment = new ResolverEnvironment();
|
||||||
|
MockPropertySource propertySource = new MockPropertySource();
|
||||||
|
propertySource.withProperty("v1", "1");
|
||||||
|
propertySource.withProperty("v2", cs);
|
||||||
|
environment.getPropertySources().addFirst(propertySource);
|
||||||
|
assertThat(environment.getProperty("v2")).isEqualTo("1");
|
||||||
|
assertThat(environment.getProperty("v2", Integer.class)).isOne();
|
||||||
|
|
||||||
|
// do not resolve to avoid ConverterNotFoundException
|
||||||
|
assertThat(environment.getProperty("v2", CharSequence.class)).isSameAs(cs);
|
||||||
|
assertThat(environment.getProperty("v2", cs.getClass())).isSameAs(cs);
|
||||||
|
}
|
||||||
|
|
||||||
private CountingMockPropertySource createMockPropertySource(StandardEnvironment environment, boolean attach) {
|
private CountingMockPropertySource createMockPropertySource(StandardEnvironment environment, boolean attach) {
|
||||||
CountingMockPropertySource propertySource = new CountingMockPropertySource();
|
CountingMockPropertySource propertySource = new CountingMockPropertySource();
|
||||||
propertySource.withProperty("spring", "boot");
|
propertySource.withProperty("spring", "boot");
|
||||||
|
Loading…
Reference in New Issue
Block a user