From 2b4cd5cf569a23aea05eba1783f6253afcbc91e4 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 12 Mar 2019 15:05:35 -0400 Subject: [PATCH] Fragment should be expanded last Closes gh-22447 --- .../web/util/HierarchicalUriComponents.java | 4 +++- .../org/springframework/web/util/UriComponentsTests.java | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java index 6800d732837..2a39c3c3b91 100644 --- a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java @@ -420,13 +420,15 @@ final class HierarchicalUriComponents extends UriComponents { Assert.state(!this.encodeState.equals(EncodeState.FULLY_ENCODED), "URI components already encoded, and could not possibly contain '{' or '}'."); + // Array-based vars rely on the below order.. + String schemeTo = expandUriComponent(getScheme(), uriVariables, this.variableEncoder); - String fragmentTo = expandUriComponent(getFragment(), uriVariables, this.variableEncoder); String userInfoTo = expandUriComponent(this.userInfo, uriVariables, this.variableEncoder); String hostTo = expandUriComponent(this.host, uriVariables, this.variableEncoder); String portTo = expandUriComponent(this.port, uriVariables, this.variableEncoder); PathComponent pathTo = this.path.expand(uriVariables, this.variableEncoder); MultiValueMap queryParamsTo = expandQueryParams(uriVariables); + String fragmentTo = expandUriComponent(getFragment(), uriVariables, this.variableEncoder); return new HierarchicalUriComponents(schemeTo, fragmentTo, userInfoTo, hostTo, portTo, pathTo, queryParamsTo, this.encodeState, this.variableEncoder); diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java index 01bd2458352..db9bb70081d 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java @@ -130,6 +130,15 @@ public class UriComponentsTests { UriComponentsBuilder.fromUriString("/myurl/?q={{{{").encode().build().toUriString()); } + @Test // gh-22447 + public void expandWithFragmentOrder() { + UriComponents uriComponents = UriComponentsBuilder + .fromUriString("http://{host}/{path}#{fragment}").build() + .expand("example.com", "foo", "bar"); + + assertEquals("http://example.com/foo#bar", uriComponents.toUriString()); + } + @Test // SPR-12123 public void port() { UriComponents uri1 = fromUriString("http://example.com:8080/bar").build();