Skip to content
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

Allow resolving nested placeholders if value is not String but CharSequence #32876

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
Allow resolving nested placeholders if value is not String but CharSe…
  • Loading branch information
quaff committed May 23, 2024
commit fb7f858a8e9c2e2a6805138146eafd195f81d494
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
*
* @author Chris Beams
* @author Juergen Hoeller
* @author Yanming Zhou
* @since 3.1
* @see PropertySource
* @see PropertySources
Expand Down Expand Up @@ -84,8 +85,8 @@ protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolv
}
Object value = propertySource.getProperty(key);
if (value != null) {
if (resolveNestedPlaceholders && value instanceof String string) {
value = resolveNestedPlaceholders(string);
if (resolveNestedPlaceholders && value instanceof CharSequence cs) {
value = resolveNestedPlaceholders(cs.toString());
}
logKeyFound(key, propertySource, value);
return convertValueIfNecessary(value, targetValueType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

/**
* @author Chris Beams
* @author Yanming Zhou
* @since 3.1
*/
class PropertySourcesPropertyResolverTests {
Expand Down Expand Up @@ -300,6 +301,43 @@ void resolveNestedPropertyPlaceholders() {
.withMessageContaining("Circular");
}

@Test
void resolveNestedPlaceholdersIfValueIsCharSequence() {
MutablePropertySources ps = new MutablePropertySources();
ps.addFirst(new MockPropertySource()
.withProperty("p1", "v1")
.withProperty("p2", "v2")
.withProperty("p3", new CharSequence() {

static final String underlying = "${p1}:${p2}";

@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;
}
})
);
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 ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
MutablePropertySources ps = new MutablePropertySources();
Expand Down