#1127 - Avoid double encoding in UriTemplate.
We now directly use UriComponentsBuilder in UriTemplate.expand(…) to avoid potential double encoding in the base URI.
This commit is contained in:
@@ -244,8 +244,7 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
return URI.create(baseUri);
|
||||
}
|
||||
|
||||
org.springframework.web.util.UriTemplate baseTemplate = new org.springframework.web.util.UriTemplate(baseUri);
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseTemplate.expand(parameters));
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUri);
|
||||
Iterator<Object> iterator = Arrays.asList(parameters).iterator();
|
||||
|
||||
for (TemplateVariable variable : getOptionalVariables()) {
|
||||
@@ -254,7 +253,7 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
appendToBuilder(builder, variable, value);
|
||||
}
|
||||
|
||||
return builder.build().toUri();
|
||||
return builder.buildAndExpand(parameters).toUri();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,20 +264,19 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
*/
|
||||
public URI expand(Map<String, ?> parameters) {
|
||||
|
||||
Assert.notNull(parameters, "Parameters must not be null!");
|
||||
|
||||
if (TemplateVariables.NONE.equals(variables)) {
|
||||
return URI.create(baseUri);
|
||||
}
|
||||
|
||||
Assert.notNull(parameters, "Parameters must not be null!");
|
||||
|
||||
org.springframework.web.util.UriTemplate baseTemplate = new org.springframework.web.util.UriTemplate(baseUri);
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseTemplate.expand(parameters));
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUri);
|
||||
|
||||
for (TemplateVariable variable : getOptionalVariables()) {
|
||||
appendToBuilder(builder, variable, parameters.get(variable.getName()));
|
||||
}
|
||||
|
||||
return builder.build().toUri();
|
||||
return builder.buildAndExpand(parameters).toUri();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -342,6 +342,13 @@ class UriTemplateUnitTest {
|
||||
assertThat(expandedTemplate).isEqualTo("/foo?bar=barExpanded&foobar=singleValue");
|
||||
}
|
||||
|
||||
@Test // #1127
|
||||
void escapesBaseUriProperly() {
|
||||
|
||||
assertThat(UriTemplate.of("https://example.org/foo and bar/{baz}").expand("xyzzy"))
|
||||
.hasToString("https://example.org/foo%20and%20bar/xyzzy");
|
||||
}
|
||||
|
||||
private static void assertVariables(UriTemplate template, TemplateVariable... variables) {
|
||||
assertVariables(template, Arrays.asList(variables));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user