Browse Source

Consistent encoding in DefaultUriBuilderFactory

Unlike 5,1, TEMPLATE_AND_VALUES is not the default encoding mode.
Nevertheless when that mode is used, it should work consistently.

Issue: SPR-17465
pull/23723/head
Rossen Stoyanchev 6 years ago
parent
commit
24051619a5
  1. 14
      spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java
  2. 10
      spring-web/src/test/java/org/springframework/web/util/DefaultUriBuilderFactoryTests.java

14
spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java

@ -228,16 +228,14 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
} }
private UriComponentsBuilder initUriComponentsBuilder(String uriTemplate) { private UriComponentsBuilder initUriComponentsBuilder(String uriTemplate) {
UriComponentsBuilder result;
if (StringUtils.isEmpty(uriTemplate)) { if (StringUtils.isEmpty(uriTemplate)) {
return baseUri != null ? baseUri.cloneBuilder() : UriComponentsBuilder.newInstance(); result = baseUri != null ? baseUri.cloneBuilder() : UriComponentsBuilder.newInstance();
} }
else if (baseUri != null) {
UriComponentsBuilder result; UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uriTemplate);
if (baseUri != null) { UriComponents uri = builder.build();
UriComponentsBuilder uricBuilder = UriComponentsBuilder.fromUriString(uriTemplate); result = uri.getHost() == null ? baseUri.cloneBuilder().uriComponents(uri) : builder;
UriComponents uric = uricBuilder.build();
result = uric.getHost() == null ? baseUri.cloneBuilder().uriComponents(uric) : uricBuilder;
} }
else { else {
result = UriComponentsBuilder.fromUriString(uriTemplate); result = UriComponentsBuilder.fromUriString(uriTemplate);

10
spring-web/src/test/java/org/springframework/web/util/DefaultUriBuilderFactoryTests.java

@ -35,7 +35,7 @@ public class DefaultUriBuilderFactoryTests {
@Test @Test
public void defaultSettings() { public void defaultSettings() {
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(); DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
URI uri = factory.uriString("/foo").pathSegment("{id}").build("a/b"); URI uri = factory.uriString("/foo/{id}").build("a/b");
assertEquals("/foo/a%2Fb", uri.toString()); assertEquals("/foo/a%2Fb", uri.toString());
} }
@ -126,6 +126,14 @@ public class DefaultUriBuilderFactoryTests {
assertEquals(expected, uriBuilder.build(singletonMap("id", id)).toString()); assertEquals(expected, uriBuilder.build(singletonMap("id", id)).toString());
} }
@Test
public void encodingTemplateAndValuesSpr17465() {
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
factory.setEncodingMode(EncodingMode.TEMPLATE_AND_VALUES);
URI uri = factory.builder().path("/foo/{id}").build("a/b");
assertEquals("/foo/a%2Fb", uri.toString());
}
@Test @Test
public void encodingValuesOnlySpr14147() { public void encodingValuesOnlySpr14147() {
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(); DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();

Loading…
Cancel
Save