diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponents.java b/spring-web/src/main/java/org/springframework/web/util/UriComponents.java index bc3281b959..a3658cb158 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponents.java @@ -243,7 +243,7 @@ public abstract class UriComponents implements Serializable { @Nullable static String expandUriComponent(@Nullable String source, UriTemplateVariables uriVariables, - @Nullable UnaryOperator variableEncoder) { + @Nullable UnaryOperator encoder) { if (source == null) { return null; @@ -258,15 +258,14 @@ public abstract class UriComponents implements Serializable { StringBuffer sb = new StringBuffer(); while (matcher.find()) { String match = matcher.group(1); - String variableName = getVariableName(match); - Object variablesValue = uriVariables.getValue(variableName); - if (UriTemplateVariables.SKIP_VALUE.equals(variablesValue)) { + String varName = getVariableName(match); + Object varValue = uriVariables.getValue(varName); + if (UriTemplateVariables.SKIP_VALUE.equals(varValue)) { continue; } - String formattedValue = getVariableValueAsString(variablesValue); - formattedValue = Matcher.quoteReplacement(formattedValue); - formattedValue = variableEncoder != null ? variableEncoder.apply(formattedValue) : formattedValue; - matcher.appendReplacement(sb, formattedValue); + String formatted = getVariableValueAsString(varValue); + formatted = encoder != null ? encoder.apply(formatted) : Matcher.quoteReplacement(formatted); + matcher.appendReplacement(sb, formatted); } matcher.appendTail(sb); return sb.toString(); diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java index 1fd87a5856..59f701fa9c 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java @@ -74,6 +74,12 @@ public class UriComponentsTests { assertEquals("/hotel%20list/Z%C3%BCrich%20specials?q=a%2Bb", uri.expand("a+b").toString()); } + @Test // SPR-17168 + public void encodeAndExpandWithDollarSign() { + UriComponents uri = UriComponentsBuilder.fromPath("/path").queryParam("q", "{value}").encode().build(); + assertEquals("/path?q=JavaClass%241.class", uri.expand("JavaClass$1.class").toString()); + } + @Test public void toUriEncoded() throws URISyntaxException { UriComponents uriComponents = UriComponentsBuilder.fromUriString(