Skip to content

HierarchicalUriComponents should format Collection URI var values in query param #34311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -47,6 +48,7 @@
* @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>
*/
Expand Down Expand Up @@ -1090,6 +1092,9 @@ public QueryUriTemplateVariables(UriTemplateVariables delegate) {
if (ObjectUtils.isArray(value)) {
value = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(value));
}
if (value instanceof Collection<?> coll) {
value = StringUtils.collectionToCommaDelimitedString(coll);
}
return value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand All @@ -42,6 +43,7 @@
* @author Arjen Poutsma
* @author Phillip Webb
* @author Rossen Stoyanchev
* @author Mengqi Xu
*/
class UriComponentsTests {

Expand Down Expand Up @@ -153,6 +155,26 @@ void expandWithFragmentOrder(ParserType parserType) {
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) {
Expand Down
Loading