Skip to content

Commit e8deba2

Browse files
committed
Fix issue with encoded params in UriComponentsBuilder
The fromUri method of UriComponentsBuilder used uri.getXxx() methods, which decode the URI parts causing URI parsing issues. The same method now uses uri.getRawXxx(). Issue: SPR-9317 Backport-Issue: SPR-9549 Backport-Commit: a33fe6f
1 parent fa33c4b commit e8deba2

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

build-spring-framework/resources/changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Changes in version 3.1.2 (2012-06-??)
2626
* fix issue with suffix pattern in AnnotationMethodHandlerAdapter
2727
* merge rather than add URI vars in ExtendedServletRequestDataBinder
2828
* raise RestClientException for unknown HTTP status codes
29-
29+
* fix issue with encoded params in UriComponentsBuilder
3030

3131
Changes in version 3.1.1 (2012-02-16)
3232
-------------------------------------

org.springframework.web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,24 +260,24 @@ public UriComponentsBuilder uri(URI uri) {
260260

261261
this.scheme = uri.getScheme();
262262

263-
if (uri.getUserInfo() != null) {
264-
this.userInfo = uri.getUserInfo();
263+
if (uri.getRawUserInfo() != null) {
264+
this.userInfo = uri.getRawUserInfo();
265265
}
266266
if (uri.getHost() != null) {
267267
this.host = uri.getHost();
268268
}
269269
if (uri.getPort() != -1) {
270270
this.port = uri.getPort();
271271
}
272-
if (StringUtils.hasLength(uri.getPath())) {
273-
this.pathBuilder = new FullPathComponentBuilder(uri.getPath());
272+
if (StringUtils.hasLength(uri.getRawPath())) {
273+
this.pathBuilder = new FullPathComponentBuilder(uri.getRawPath());
274274
}
275-
if (StringUtils.hasLength(uri.getQuery())) {
275+
if (StringUtils.hasLength(uri.getRawQuery())) {
276276
this.queryParams.clear();
277-
query(uri.getQuery());
277+
query(uri.getRawQuery());
278278
}
279-
if (uri.getFragment() != null) {
280-
this.fragment = uri.getFragment();
279+
if (uri.getRawFragment() != null) {
280+
this.fragment = uri.getRawFragment();
281281
}
282282
return this;
283283
}

org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ public void fromUri() throws URISyntaxException {
7676
assertEquals("Invalid result URI", uri, result.toUri());
7777
}
7878

79+
// SPR-9317
80+
81+
@Test
82+
public void fromUriEncodedQuery() throws URISyntaxException {
83+
URI uri = new URI("http://www.example.org/?param=aGVsbG9Xb3JsZA%3D%3D");
84+
String fromUri = UriComponentsBuilder.fromUri(uri).build().getQueryParams().get("param").get(0);
85+
String fromUriString = UriComponentsBuilder.fromUriString(uri.toString()).build().getQueryParams().get("param").get(0);
86+
87+
assertEquals(fromUri, fromUriString);
88+
}
89+
7990
@Test
8091
public void fromUriString() {
8192
UriComponents result = UriComponentsBuilder.fromUriString("http://www.ietf.org/rfc/rfc3986.txt").build();

0 commit comments

Comments
 (0)