Browse Source

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
pull/22245/head
Rossen Stoyanchev 6 years ago
parent
commit
b219c6ce15
  1. 14
      spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

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

@ -441,7 +441,15 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable { @@ -441,7 +441,15 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
}
/**
* Build a URI String. This is a shortcut for:
* Build a URI String.
* <p>Effectively, a shortcut for building, encoding, and returning the
* String representation:
* <pre class="code">
* String uri = builder.build().encode().toUriString()
* </pre>
* <p>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:
* <pre>
* String uri = builder.encode().build().toUriString()
* </pre>
@ -449,7 +457,9 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable { @@ -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();
}

Loading…
Cancel
Save