From e4c84ec7576bc2986357b5bf12b646d663ef9730 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 9 Nov 2018 14:13:51 -0500 Subject: [PATCH] Consistent default encoding in DefaultUriBuilderFactory Issue: SPR-17465 --- .../web/util/DefaultUriBuilderFactory.java | 14 ++++++-------- .../web/util/DefaultUriBuilderFactoryTests.java | 9 ++++++++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java b/spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java index 8a3205ca47..96ae617d84 100644 --- a/spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java +++ b/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) { - + UriComponentsBuilder result; if (StringUtils.isEmpty(uriTemplate)) { - return baseUri != null ? baseUri.cloneBuilder() : UriComponentsBuilder.newInstance(); + result = baseUri != null ? baseUri.cloneBuilder() : UriComponentsBuilder.newInstance(); } - - UriComponentsBuilder result; - if (baseUri != null) { - UriComponentsBuilder uricBuilder = UriComponentsBuilder.fromUriString(uriTemplate); - UriComponents uric = uricBuilder.build(); - result = uric.getHost() == null ? baseUri.cloneBuilder().uriComponents(uric) : uricBuilder; + else if (baseUri != null) { + UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uriTemplate); + UriComponents uri = builder.build(); + result = uri.getHost() == null ? baseUri.cloneBuilder().uriComponents(uri) : builder; } else { result = UriComponentsBuilder.fromUriString(uriTemplate); diff --git a/spring-web/src/test/java/org/springframework/web/util/DefaultUriBuilderFactoryTests.java b/spring-web/src/test/java/org/springframework/web/util/DefaultUriBuilderFactoryTests.java index 9e961d1967..c0a99755d6 100644 --- a/spring-web/src/test/java/org/springframework/web/util/DefaultUriBuilderFactoryTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/DefaultUriBuilderFactoryTests.java @@ -35,7 +35,14 @@ public class DefaultUriBuilderFactoryTests { @Test public void defaultSettings() { 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()); + } + + @Test // SPR-17465 + public void defaultSettingsWithBuilder() { + DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(); + URI uri = factory.builder().path("/foo/{id}").build("a/b"); assertEquals("/foo/a%2Fb", uri.toString()); }