Browse Source

UriComponentsBuilder copies query params through MultiValueMap.addAll

Issue: SPR-17256
pull/1998/head
Juergen Hoeller 7 years ago
parent
commit
1d58fac54d
  1. 10
      spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java
  2. 2
      spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java
  3. 16
      spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -117,7 +117,6 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa @@ -117,7 +117,6 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
public Map<K, V> toSingleValueMap() {
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.targetMap.size());
this.targetMap.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
return singleValueMap;
}
@ -191,7 +190,10 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa @@ -191,7 +190,10 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
/**
* Create a deep copy of this Map.
* @return a copy of this Map, including a copy of each value-holding List entry
* (consistently using an independent modifiable {@link LinkedList} for each entry)
* along the lines of {@code MultiValueMap.addAll} semantics
* @since 4.2
* @see #addAll(MultiValueMap)
* @see #clone()
*/
public LinkedMultiValueMap<K, V> deepCopy() {
@ -203,7 +205,11 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa @@ -203,7 +205,11 @@ public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializa
/**
* Create a regular copy of this Map.
* @return a shallow copy of this Map, reusing this Map's value-holding List entries
* (even if some entries are shared or unmodifiable) along the lines of standard
* {@code Map.put} semantics
* @since 4.2
* @see #put(Object, List)
* @see #putAll(Map)
* @see LinkedMultiValueMap#LinkedMultiValueMap(Map)
* @see #deepCopy()
*/

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

@ -705,7 +705,7 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable { @@ -705,7 +705,7 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
@Override
public UriComponentsBuilder queryParams(@Nullable MultiValueMap<String, String> params) {
if (params != null) {
this.queryParams.putAll(params);
this.queryParams.addAll(params);
}
return this;
}

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

@ -36,13 +36,15 @@ import static org.hamcrest.Matchers.*; @@ -36,13 +36,15 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Unit tests for {@link org.springframework.web.util.UriComponentsBuilder}.
* Unit tests for {@link UriComponentsBuilder}.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @author Phillip Webb
* @author Oliver Gierke
* @author David Eckel
* @author Juergen Hoeller
* @author Sam Brannen
* @author David Eckel
*/
public class UriComponentsBuilderTests {
@ -902,9 +904,19 @@ public class UriComponentsBuilderTests { @@ -902,9 +904,19 @@ public class UriComponentsBuilderTests {
public void uriComponentsNotEqualAfterNormalization() {
UriComponents uri1 = UriComponentsBuilder.fromUriString("http://test.com").build().normalize();
UriComponents uri2 = UriComponentsBuilder.fromUriString("http://test.com/").build();
assertTrue(uri1.getPathSegments().isEmpty());
assertTrue(uri2.getPathSegments().isEmpty());
assertNotEquals(uri1, uri2);
}
@Test // SPR-17256
public void uriComponentsWithMergedQueryParams() {
String uri = UriComponentsBuilder.fromUriString("http://localhost:8081")
.uriComponents(UriComponentsBuilder.fromUriString("/{path}?sort={sort}").build())
.queryParam("sort", "another_value").build().toString();
assertEquals("http://localhost:8081/{path}?sort={sort}&sort=another_value", uri);
}
}

Loading…
Cancel
Save