Skip to content

Commit c6e5008

Browse files
committed
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
1 parent 7a88e6d commit c6e5008

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,25 @@ public URI build(Map<String, ?> uriVariables) {
441441
}
442442

443443
/**
444-
* Build a URI String. This is a shortcut for:
444+
* Build a URI String.
445+
* <p>Effectively, a shortcut for building, encoding, and returning the
446+
* String representation:
447+
* <pre class="code">
448+
* String uri = builder.build().encode().toUriString()
449+
* </pre>
450+
* <p>However if {@link #uriVariables(Map) URI variables} have been provided
451+
* then the URI template is pre-encoded separately from URI variables (see
452+
* {@link #encode()} for details), i.e. equivalent to:
445453
* <pre>
446454
* String uri = builder.encode().build().toUriString()
447455
* </pre>
448456
* @since 4.1
449457
* @see UriComponents#toUriString()
450458
*/
451459
public String toUriString() {
452-
return buildInternal(EncodingHint.ENCODE_TEMPLATE).toUriString();
460+
return this.uriVariables.isEmpty() ?
461+
encode().build().toUriString() :
462+
buildInternal(EncodingHint.ENCODE_TEMPLATE).toUriString();
453463
}
454464

455465

0 commit comments

Comments
 (0)