Browse Source

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
pull/1946/head
Rossen Stoyanchev 7 years ago
parent
commit
f23496ae32
  1. 15
      spring-web/src/main/java/org/springframework/web/util/UriComponents.java
  2. 6
      spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java

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

@ -243,7 +243,7 @@ public abstract class UriComponents implements Serializable {
@Nullable @Nullable
static String expandUriComponent(@Nullable String source, UriTemplateVariables uriVariables, static String expandUriComponent(@Nullable String source, UriTemplateVariables uriVariables,
@Nullable UnaryOperator<String> variableEncoder) { @Nullable UnaryOperator<String> encoder) {
if (source == null) { if (source == null) {
return null; return null;
@ -258,15 +258,14 @@ public abstract class UriComponents implements Serializable {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
while (matcher.find()) { while (matcher.find()) {
String match = matcher.group(1); String match = matcher.group(1);
String variableName = getVariableName(match); String varName = getVariableName(match);
Object variablesValue = uriVariables.getValue(variableName); Object varValue = uriVariables.getValue(varName);
if (UriTemplateVariables.SKIP_VALUE.equals(variablesValue)) { if (UriTemplateVariables.SKIP_VALUE.equals(varValue)) {
continue; continue;
} }
String formattedValue = getVariableValueAsString(variablesValue); String formatted = getVariableValueAsString(varValue);
formattedValue = Matcher.quoteReplacement(formattedValue); formatted = encoder != null ? encoder.apply(formatted) : Matcher.quoteReplacement(formatted);
formattedValue = variableEncoder != null ? variableEncoder.apply(formattedValue) : formattedValue; matcher.appendReplacement(sb, formatted);
matcher.appendReplacement(sb, formattedValue);
} }
matcher.appendTail(sb); matcher.appendTail(sb);
return sb.toString(); return sb.toString();

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

@ -78,6 +78,12 @@ public class UriComponentsTests {
assertEquals("/hotel%20list/Z%C3%BCrich%20specials?q=a%2Bb", uri.expand("a+b").toString()); 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 @Test
public void toUriEncoded() throws URISyntaxException { public void toUriEncoded() throws URISyntaxException {
UriComponents uriComponents = UriComponentsBuilder.fromUriString( UriComponents uriComponents = UriComponentsBuilder.fromUriString(

Loading…
Cancel
Save