From 07653bfd0c0f6996e299aac51f300c465567bcc5 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 19 Jul 2018 18:56:08 -0400 Subject: [PATCH] Fix for encoding issue with MvcUriComponentsBuilder Provide method for stronger encoding of expanded URI variables when building links from views. Issue: SPR-17027 --- .../method/annotation/MvcUriComponentsBuilder.java | 14 ++++++++++++-- .../annotation/MvcUriComponentsBuilderTests.java | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java index 844c61df01..5c025eefaf 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java @@ -868,14 +868,24 @@ public class MvcUriComponentsBuilder { return this; } + /** + * Use this method only if you need to apply strong encoding to expanded + * URI variables by quoting all characters with reserved meaning. + * @since 5.0.8 + */ + public MethodArgumentBuilder encode() { + this.baseUrl.encode(); + return this; + } + public String build() { return fromMethodInternal(this.baseUrl, this.controllerType, this.method, this.argumentValues) - .build(false).encode().toUriString(); + .build().encode().toUriString(); } public String buildAndExpand(Object... uriVars) { return fromMethodInternal(this.baseUrl, this.controllerType, this.method, this.argumentValues) - .build(false).expand(uriVars).encode().toString(); + .buildAndExpand(uriVars).encode().toString(); } } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java index a800126e80..6faf920239 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java @@ -402,6 +402,20 @@ public class MvcUriComponentsBuilderTests { assertEquals("http://example.org:9999/base/people/123/addresses/DE", url); } + @Test // SPR-17027 + public void fromMappingNameWithEncoding() { + + initWebApplicationContext(WebConfig.class); + + this.request.setServerName("example.org"); + this.request.setServerPort(9999); + this.request.setContextPath("/base"); + + String mappingName = "PAC#getAddressesForCountry"; + String url = fromMappingName(mappingName).arg(0, "DE;FR").encode().buildAndExpand("_+_"); + assertEquals("/base/people/_%2B_/addresses/DE%3BFR", url); + } + @Test public void fromControllerWithPrefix() {