Skip to content

Commit 1d58fac

Browse files
committed
UriComponentsBuilder copies query params through MultiValueMap.addAll
Issue: SPR-17256
1 parent 658bd7a commit 1d58fac

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -117,7 +117,6 @@ public void setAll(Map<K, V> values) {
117117
public Map<K, V> toSingleValueMap() {
118118
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.targetMap.size());
119119
this.targetMap.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
120-
121120
return singleValueMap;
122121
}
123122

@@ -191,7 +190,10 @@ public Set<Entry<K, List<V>>> entrySet() {
191190
/**
192191
* Create a deep copy of this Map.
193192
* @return a copy of this Map, including a copy of each value-holding List entry
193+
* (consistently using an independent modifiable {@link LinkedList} for each entry)
194+
* along the lines of {@code MultiValueMap.addAll} semantics
194195
* @since 4.2
196+
* @see #addAll(MultiValueMap)
195197
* @see #clone()
196198
*/
197199
public LinkedMultiValueMap<K, V> deepCopy() {
@@ -203,7 +205,11 @@ public LinkedMultiValueMap<K, V> deepCopy() {
203205
/**
204206
* Create a regular copy of this Map.
205207
* @return a shallow copy of this Map, reusing this Map's value-holding List entries
208+
* (even if some entries are shared or unmodifiable) along the lines of standard
209+
* {@code Map.put} semantics
206210
* @since 4.2
211+
* @see #put(Object, List)
212+
* @see #putAll(Map)
207213
* @see LinkedMultiValueMap#LinkedMultiValueMap(Map)
208214
* @see #deepCopy()
209215
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ public UriComponentsBuilder queryParam(String name, Object... values) {
705705
@Override
706706
public UriComponentsBuilder queryParams(@Nullable MultiValueMap<String, String> params) {
707707
if (params != null) {
708-
this.queryParams.putAll(params);
708+
this.queryParams.addAll(params);
709709
}
710710
return this;
711711
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@
3636
import static org.junit.Assert.*;
3737

3838
/**
39-
* Unit tests for {@link org.springframework.web.util.UriComponentsBuilder}.
39+
* Unit tests for {@link UriComponentsBuilder}.
4040
*
4141
* @author Arjen Poutsma
42+
* @author Rossen Stoyanchev
4243
* @author Phillip Webb
4344
* @author Oliver Gierke
44-
* @author David Eckel
45+
* @author Juergen Hoeller
4546
* @author Sam Brannen
47+
* @author David Eckel
4648
*/
4749
public class UriComponentsBuilderTests {
4850

@@ -902,9 +904,19 @@ public void fromHttpRequestForwardedHeaderWithProtoAndServerPort() {
902904
public void uriComponentsNotEqualAfterNormalization() {
903905
UriComponents uri1 = UriComponentsBuilder.fromUriString("http://test.com").build().normalize();
904906
UriComponents uri2 = UriComponentsBuilder.fromUriString("http://test.com/").build();
907+
905908
assertTrue(uri1.getPathSegments().isEmpty());
906909
assertTrue(uri2.getPathSegments().isEmpty());
907910
assertNotEquals(uri1, uri2);
908911
}
909912

913+
@Test // SPR-17256
914+
public void uriComponentsWithMergedQueryParams() {
915+
String uri = UriComponentsBuilder.fromUriString("http://localhost:8081")
916+
.uriComponents(UriComponentsBuilder.fromUriString("/{path}?sort={sort}").build())
917+
.queryParam("sort", "another_value").build().toString();
918+
919+
assertEquals("http://localhost:8081/{path}?sort={sort}&sort=another_value", uri);
920+
}
921+
910922
}

0 commit comments

Comments
 (0)