Skip to content

Commit 295a956

Browse files
remeiorstoyanchev
authored andcommitted
Format Collection query param in UriComponentsBuilder
See gh-34311 Signed-off-by: Mengqi Xu <[email protected]>
1 parent b07ff1c commit 295a956

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.net.URISyntaxException;
2323
import java.nio.charset.Charset;
2424
import java.util.ArrayList;
25+
import java.util.Collection;
2526
import java.util.Collections;
2627
import java.util.List;
2728
import java.util.Objects;
@@ -47,6 +48,7 @@
4748
* @author Rossen Stoyanchev
4849
* @author Phillip Webb
4950
* @author Sam Brannen
51+
* @author Mengqi Xu
5052
* @since 3.1.3
5153
* @see <a href="https://tools.ietf.org/html/rfc3986#section-1.2.3">Hierarchical URIs</a>
5254
*/
@@ -1090,6 +1092,9 @@ public QueryUriTemplateVariables(UriTemplateVariables delegate) {
10901092
if (ObjectUtils.isArray(value)) {
10911093
value = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(value));
10921094
}
1095+
if (value instanceof Collection<?> coll) {
1096+
value = StringUtils.collectionToCommaDelimitedString(coll);
1097+
}
10931098
return value;
10941099
}
10951100
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.URI;
2424
import java.util.Arrays;
2525
import java.util.Collections;
26+
import java.util.List;
2627

2728
import org.junit.jupiter.api.Test;
2829
import org.junit.jupiter.params.ParameterizedTest;
@@ -42,6 +43,7 @@
4243
* @author Arjen Poutsma
4344
* @author Phillip Webb
4445
* @author Rossen Stoyanchev
46+
* @author Mengqi Xu
4547
*/
4648
class UriComponentsTests {
4749

@@ -153,6 +155,26 @@ void expandWithFragmentOrder(ParserType parserType) {
153155
assertThat(uri.toUriString()).isEqualTo("https://example.com/foo#bar");
154156
}
155157

158+
@Test
159+
void expandQueryParamWithArray() {
160+
UriComponents uri = UriComponentsBuilder.fromPath("/hello")
161+
.queryParam("name", "{name}")
162+
.build();
163+
uri = uri.expand(Collections.singletonMap("name", new String[]{"foo", "bar"}));
164+
165+
assertThat(uri.toString()).hasToString("/hello?name=foo,bar");
166+
}
167+
168+
@Test
169+
void expandQueryParamWithList() {
170+
UriComponents uri = UriComponentsBuilder.fromPath("/hello")
171+
.queryParam("name", "{name}")
172+
.build();
173+
uri = uri.expand(Collections.singletonMap("name", List.of("foo", "bar")));
174+
175+
assertThat(uri.toString()).hasToString("/hello?name=foo,bar");
176+
}
177+
156178
@ParameterizedTest // SPR-12123
157179
@EnumSource
158180
void port(ParserType parserType) {

0 commit comments

Comments
 (0)