Skip to content

Resolve nested placeholders as possible if value is CharSequence #40862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* underlying sources if the name is a value {@link ConfigurationPropertyName}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResolver {

Expand Down Expand Up @@ -76,8 +77,16 @@ private <T> T getProperty(String key, Class<T> targetValueType, boolean resolveN
if (value == null) {
return null;
}
if (resolveNestedPlaceholders && value instanceof String string) {
value = resolveNestedPlaceholders(string);
if (resolveNestedPlaceholders) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,7 @@
* Tests for {@link ConfigurationPropertySourcesPropertyResolver}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
class ConfigurationPropertySourcesPropertyResolverTests {

Expand Down Expand Up @@ -113,6 +114,45 @@ void getPropertyAsTypeWhenHasPlaceholder() {
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) {
CountingMockPropertySource propertySource = new CountingMockPropertySource();
propertySource.withProperty("spring", "boot");
Expand Down