Skip to content

Commit 07653bf

Browse files
committed
Fix for encoding issue with MvcUriComponentsBuilder
Provide method for stronger encoding of expanded URI variables when building links from views. Issue: SPR-17027
1 parent 93b7a48 commit 07653bf

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,14 +868,24 @@ public MethodArgumentBuilder arg(int index, Object value) {
868868
return this;
869869
}
870870

871+
/**
872+
* Use this method only if you need to apply strong encoding to expanded
873+
* URI variables by quoting all characters with reserved meaning.
874+
* @since 5.0.8
875+
*/
876+
public MethodArgumentBuilder encode() {
877+
this.baseUrl.encode();
878+
return this;
879+
}
880+
871881
public String build() {
872882
return fromMethodInternal(this.baseUrl, this.controllerType, this.method, this.argumentValues)
873-
.build(false).encode().toUriString();
883+
.build().encode().toUriString();
874884
}
875885

876886
public String buildAndExpand(Object... uriVars) {
877887
return fromMethodInternal(this.baseUrl, this.controllerType, this.method, this.argumentValues)
878-
.build(false).expand(uriVars).encode().toString();
888+
.buildAndExpand(uriVars).encode().toString();
879889
}
880890
}
881891

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,20 @@ public void fromMappingNameWithCustomBaseUrl() {
402402
assertEquals("http://example.org:9999/base/people/123/addresses/DE", url);
403403
}
404404

405+
@Test // SPR-17027
406+
public void fromMappingNameWithEncoding() {
407+
408+
initWebApplicationContext(WebConfig.class);
409+
410+
this.request.setServerName("example.org");
411+
this.request.setServerPort(9999);
412+
this.request.setContextPath("/base");
413+
414+
String mappingName = "PAC#getAddressesForCountry";
415+
String url = fromMappingName(mappingName).arg(0, "DE;FR").encode().buildAndExpand("_+_");
416+
assertEquals("/base/people/_%2B_/addresses/DE%3BFR", url);
417+
}
418+
405419
@Test
406420
public void fromControllerWithPrefix() {
407421

0 commit comments

Comments
 (0)