From b219c6ce15637a7a190a657f864e631e428b5378 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 8 Jan 2019 11:02:55 -0500 Subject: [PATCH] Adjust UriComponentsBuilder#toUriString behavior Commit #93b7a4 added support for pre-configuring URI variables at the UriComponentsBuilder level, and also changed toUriString to encode template and URI variables separately. However this went a bit too far causing side effects for URLs with curly braces that don't represent URI variables. This commit restores the original toUriString behavior which is to encode template and URI variables sepraately only if URI variables have been pre-configured. Issue: SPR-17630 --- .../web/util/UriComponentsBuilder.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java index f33c8b2e609..8e94a0304d6 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java @@ -441,7 +441,15 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable { } /** - * Build a URI String. This is a shortcut for: + * Build a URI String. + *

Effectively, a shortcut for building, encoding, and returning the + * String representation: + *

+	 * String uri = builder.build().encode().toUriString()
+	 * 
+ *

However if {@link #uriVariables(Map) URI variables} have been provided + * then the URI template is pre-encoded separately from URI variables (see + * {@link #encode()} for details), i.e. equivalent to: *

 	 * String uri = builder.encode().build().toUriString()
 	 * 
@@ -449,7 +457,9 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable { * @see UriComponents#toUriString() */ public String toUriString() { - return buildInternal(EncodingHint.ENCODE_TEMPLATE).toUriString(); + return this.uriVariables.isEmpty() ? + encode().build().toUriString() : + buildInternal(EncodingHint.ENCODE_TEMPLATE).toUriString(); }