Skip to content

Fix: preserve nested escaped placeholders during resolution #34720

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -18,6 +18,11 @@

import org.jspecify.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* {@link PropertyResolver} implementation that resolves property values against
* an underlying set of {@link PropertySources}.
Expand Down Expand Up @@ -81,11 +86,11 @@ public boolean containsProperty(String key) {
if (value != null) {
if (resolveNestedPlaceholders) {
if (value instanceof String string) {
value = resolveNestedPlaceholders(string);
value = resolveNestedPlaceholdersWithEscapePreservation(string);
}
else if ((value instanceof CharSequence cs) && (String.class.equals(targetValueType) ||
CharSequence.class.equals(targetValueType))) {
value = resolveNestedPlaceholders(cs.toString());
value = resolveNestedPlaceholdersWithEscapePreservation(cs.toString());
}
}
logKeyFound(key, propertySource, value);
Expand All @@ -99,6 +104,42 @@ else if ((value instanceof CharSequence cs) && (String.class.equals(targetValueT
return null;
}

/**
* Resolves nested placeholders while preserving escaped placeholders.
* This method ensures that escaped placeholders like \\${...} remain as literal ${...}
* in the final output without being evaluated.
*
* @param value the value containing possible placeholders
* @return the resolved value with escaped placeholders preserved
*/
private String resolveNestedPlaceholdersWithEscapePreservation(String value) {
final String ESCAPE_MARKER = "__ESCAPED_PH_MARKER__";

Pattern escapedPattern = Pattern.compile("\\\\(\\$\\{[^}]+\\})");
Matcher escapedMatcher = escapedPattern.matcher(value);
StringBuilder tempBuffer = new StringBuilder();

List<String> escapedValues = new ArrayList<>();
while (escapedMatcher.find()) {
String fullPlaceholder = escapedMatcher.group(1);
escapedValues.add(fullPlaceholder);
escapedMatcher.appendReplacement(tempBuffer, ESCAPE_MARKER + (escapedValues.size() - 1));
}
escapedMatcher.appendTail(tempBuffer);
String markedValue = tempBuffer.toString();

String resolvedValue = resolveNestedPlaceholders(markedValue);

for (int i = 0; i < escapedValues.size(); i++) {
resolvedValue = resolvedValue.replace(
ESCAPE_MARKER + i,
escapedValues.get(i)
);
}

return resolvedValue;
}

/**
* Log the given key as found in the given {@link PropertySource}, resulting in
* the given value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,56 @@ void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
}

@Test
void escapedPlaceholders_areNotEvaluated() {
testProperties.put("prop1", "value1");
testProperties.put("prop2", "value2\\${prop1}");

assertThat(propertyResolver.getProperty("prop2")).isEqualTo("value2${prop1}");
}

@Test
void multipleEscapedPlaceholders_arePreserved() {
testProperties.put("prop1", "value1");
testProperties.put("prop2", "value2");
testProperties.put("complex", "start\\${prop1}middle\\${prop2}end");

assertThat(propertyResolver.getProperty("complex")).isEqualTo("start${prop1}middle${prop2}end");
}

@Test
void doubleBackslashes_areProcessedCorrectly() {
testProperties.put("prop1", "value1");
testProperties.put("doubleEscaped", "value2\\\\${prop1}");

assertThat(propertyResolver.getProperty("doubleEscaped")).isEqualTo("value2\\${prop1}");
}

@Test
void escapedPlaceholdersInNestedProperties() {
MutablePropertySources ps = new MutablePropertySources();
ps.addFirst(new MockPropertySource()
.withProperty("p1", "v1")
.withProperty("p2", "v2")
.withProperty("escaped", "prefix-\\${p1}")
.withProperty("nested", "${escaped}-${p2}")
);
ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps);

assertThat(pr.getProperty("nested")).isEqualTo("prefix-${p1}-v2");
}


@Test
void escapedPlaceholders_withCharSequenceValues() {
MutablePropertySources ps = new MutablePropertySources();
ps.addFirst(new MockPropertySource()
.withProperty("p1", "v1")
.withProperty("charseq", new StringBuilder("prefix-\\${p1}"))
);
PropertyResolver resolver = new PropertySourcesPropertyResolver(ps);

assertThat(resolver.getProperty("charseq")).isEqualTo("prefix-${p1}");
}

}