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/1919/merge
Rossen Stoyanchev 6 years ago
parent
commit
99f0129711
  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 { @@ -243,7 +243,7 @@ public abstract class UriComponents implements Serializable {
@Nullable
static String expandUriComponent(@Nullable String source, UriTemplateVariables uriVariables,
@Nullable UnaryOperator<String> variableEncoder) {
@Nullable UnaryOperator<String> encoder) {
if (source == null) {
return null;
@ -258,15 +258,14 @@ public abstract class UriComponents implements Serializable { @@ -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();

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

@ -74,6 +74,12 @@ public class UriComponentsTests { @@ -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(

Loading…
Cancel
Save