Browse Source

Format Collection query param in UriComponentsBuilder

See gh-34311

Signed-off-by: Mengqi Xu <2663479778@qq.com>
pull/34429/head
Mengqi Xu 11 months ago committed by rstoyanchev
parent
commit
295a9565a3
  1. 5
      spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java
  2. 22
      spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java

5
spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java

@ -22,6 +22,7 @@ import java.net.URI; @@ -22,6 +22,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
@ -47,6 +48,7 @@ import org.springframework.util.StringUtils; @@ -47,6 +48,7 @@ import org.springframework.util.StringUtils;
* @author Rossen Stoyanchev
* @author Phillip Webb
* @author Sam Brannen
* @author Mengqi Xu
* @since 3.1.3
* @see <a href="https://tools.ietf.org/html/rfc3986#section-1.2.3">Hierarchical URIs</a>
*/
@ -1090,6 +1092,9 @@ final class HierarchicalUriComponents extends UriComponents { @@ -1090,6 +1092,9 @@ final class HierarchicalUriComponents extends UriComponents {
if (ObjectUtils.isArray(value)) {
value = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(value));
}
if (value instanceof Collection<?> coll) {
value = StringUtils.collectionToCommaDelimitedString(coll);
}
return value;
}
}

22
spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java

@ -23,6 +23,7 @@ import java.io.ObjectOutputStream; @@ -23,6 +23,7 @@ import java.io.ObjectOutputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@ -42,6 +43,7 @@ import static org.springframework.web.util.UriComponentsBuilder.fromUriString; @@ -42,6 +43,7 @@ import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
* @author Arjen Poutsma
* @author Phillip Webb
* @author Rossen Stoyanchev
* @author Mengqi Xu
*/
class UriComponentsTests {
@ -153,6 +155,26 @@ class UriComponentsTests { @@ -153,6 +155,26 @@ class UriComponentsTests {
assertThat(uri.toUriString()).isEqualTo("https://example.com/foo#bar");
}
@Test
void expandQueryParamWithArray() {
UriComponents uri = UriComponentsBuilder.fromPath("/hello")
.queryParam("name", "{name}")
.build();
uri = uri.expand(Collections.singletonMap("name", new String[]{"foo", "bar"}));
assertThat(uri.toString()).hasToString("/hello?name=foo,bar");
}
@Test
void expandQueryParamWithList() {
UriComponents uri = UriComponentsBuilder.fromPath("/hello")
.queryParam("name", "{name}")
.build();
uri = uri.expand(Collections.singletonMap("name", List.of("foo", "bar")));
assertThat(uri.toString()).hasToString("/hello?name=foo,bar");
}
@ParameterizedTest // SPR-12123
@EnumSource
void port(ParserType parserType) {

Loading…
Cancel
Save