Skip to content

Commit f23496a

Browse files
committed
Fix URI var encoding issue with '$'
When expanding and strictly encoding URI variables, there is no need to quote `/` and `$` which will be encoded anyway. Issue: SPR-17168
1 parent 1d8e5f4 commit f23496a

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

spring-web/src/main/java/org/springframework/web/util/UriComponents.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static String expandUriComponent(@Nullable String source, UriTemplateVariables u
243243

244244
@Nullable
245245
static String expandUriComponent(@Nullable String source, UriTemplateVariables uriVariables,
246-
@Nullable UnaryOperator<String> variableEncoder) {
246+
@Nullable UnaryOperator<String> encoder) {
247247

248248
if (source == null) {
249249
return null;
@@ -258,15 +258,14 @@ static String expandUriComponent(@Nullable String source, UriTemplateVariables u
258258
StringBuffer sb = new StringBuffer();
259259
while (matcher.find()) {
260260
String match = matcher.group(1);
261-
String variableName = getVariableName(match);
262-
Object variablesValue = uriVariables.getValue(variableName);
263-
if (UriTemplateVariables.SKIP_VALUE.equals(variablesValue)) {
261+
String varName = getVariableName(match);
262+
Object varValue = uriVariables.getValue(varName);
263+
if (UriTemplateVariables.SKIP_VALUE.equals(varValue)) {
264264
continue;
265265
}
266-
String formattedValue = getVariableValueAsString(variablesValue);
267-
formattedValue = Matcher.quoteReplacement(formattedValue);
268-
formattedValue = variableEncoder != null ? variableEncoder.apply(formattedValue) : formattedValue;
269-
matcher.appendReplacement(sb, formattedValue);
266+
String formatted = getVariableValueAsString(varValue);
267+
formatted = encoder != null ? encoder.apply(formatted) : Matcher.quoteReplacement(formatted);
268+
matcher.appendReplacement(sb, formatted);
270269
}
271270
matcher.appendTail(sb);
272271
return sb.toString();

spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public void encodeAndExpandPartially() {
7878
assertEquals("/hotel%20list/Z%C3%BCrich%20specials?q=a%2Bb", uri.expand("a+b").toString());
7979
}
8080

81+
@Test // SPR-17168
82+
public void encodeAndExpandWithDollarSign() {
83+
UriComponents uri = UriComponentsBuilder.fromPath("/path").queryParam("q", "{value}").encode().build();
84+
assertEquals("/path?q=JavaClass%241.class", uri.expand("JavaClass$1.class").toString());
85+
}
86+
8187
@Test
8288
public void toUriEncoded() throws URISyntaxException {
8389
UriComponents uriComponents = UriComponentsBuilder.fromUriString(

0 commit comments

Comments
 (0)