Skip to content

Commit 2b4cd5c

Browse files
committed
Fragment should be expanded last
Closes gh-22447
1 parent 7dc522f commit 2b4cd5c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,15 @@ protected HierarchicalUriComponents expandInternal(UriTemplateVariables uriVaria
420420
Assert.state(!this.encodeState.equals(EncodeState.FULLY_ENCODED),
421421
"URI components already encoded, and could not possibly contain '{' or '}'.");
422422

423+
// Array-based vars rely on the below order..
424+
423425
String schemeTo = expandUriComponent(getScheme(), uriVariables, this.variableEncoder);
424-
String fragmentTo = expandUriComponent(getFragment(), uriVariables, this.variableEncoder);
425426
String userInfoTo = expandUriComponent(this.userInfo, uriVariables, this.variableEncoder);
426427
String hostTo = expandUriComponent(this.host, uriVariables, this.variableEncoder);
427428
String portTo = expandUriComponent(this.port, uriVariables, this.variableEncoder);
428429
PathComponent pathTo = this.path.expand(uriVariables, this.variableEncoder);
429430
MultiValueMap<String, String> queryParamsTo = expandQueryParams(uriVariables);
431+
String fragmentTo = expandUriComponent(getFragment(), uriVariables, this.variableEncoder);
430432

431433
return new HierarchicalUriComponents(schemeTo, fragmentTo, userInfoTo,
432434
hostTo, portTo, pathTo, queryParamsTo, this.encodeState, this.variableEncoder);

spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ public void uirTemplateExpandWithMismatchedCurlyBraces() {
130130
UriComponentsBuilder.fromUriString("/myurl/?q={{{{").encode().build().toUriString());
131131
}
132132

133+
@Test // gh-22447
134+
public void expandWithFragmentOrder() {
135+
UriComponents uriComponents = UriComponentsBuilder
136+
.fromUriString("http://{host}/{path}#{fragment}").build()
137+
.expand("example.com", "foo", "bar");
138+
139+
assertEquals("http://example.com/foo#bar", uriComponents.toUriString());
140+
}
141+
133142
@Test // SPR-12123
134143
public void port() {
135144
UriComponents uri1 = fromUriString("http://example.com:8080/bar").build();

0 commit comments

Comments
 (0)