From e66e34766e94fe4ab99af7b178ae2eb3bbff5a48 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 23 Oct 2020 18:08:54 +0100 Subject: [PATCH] Support Optional in UriComponentsBuilder#queryParam Closes gh-25951 --- .../web/util/UriComponentsBuilder.java | 12 +++++++++- .../web/util/UriComponentsBuilderTests.java | 22 ++++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java index b76798e6f1c5..c57f77bc5057 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java @@ -698,7 +698,7 @@ public UriComponentsBuilder queryParam(String name, Object... values) { Assert.notNull(name, "Name must not be null"); if (!ObjectUtils.isEmpty(values)) { for (Object value : values) { - String valueAsString = (value != null ? value.toString() : null); + String valueAsString = getQueryParamValue(value); this.queryParams.add(name, valueAsString); } } @@ -709,6 +709,16 @@ public UriComponentsBuilder queryParam(String name, Object... values) { return this; } + @Nullable + private String getQueryParamValue(@Nullable Object value) { + if (value != null) { + return (value instanceof Optional ? + ((Optional) value).map(Object::toString).orElse(null) : + value.toString()); + } + return null; + } + @Override public UriComponentsBuilder queryParam(String name, @Nullable Collection values) { return queryParam(name, (CollectionUtils.isEmpty(values) ? EMPTY_VALUES : values.toArray())); diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index 93f77aa3e930..c22e21753e83 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -749,14 +749,24 @@ void queryParam() { @Test void queryParamWithList() { - UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); - UriComponents result = builder.queryParam("baz", Arrays.asList("qux", 42)).build(); + List values = Arrays.asList("qux", "42"); + UriComponents result = UriComponentsBuilder.newInstance().queryParam("baz", values).build(); assertThat(result.getQuery()).isEqualTo("baz=qux&baz=42"); - MultiValueMap expectedQueryParams = new LinkedMultiValueMap<>(2); - expectedQueryParams.add("baz", "qux"); - expectedQueryParams.add("baz", "42"); - assertThat(result.getQueryParams()).isEqualTo(expectedQueryParams); + assertThat(result.getQueryParams()).containsOnlyKeys("baz").containsEntry("baz", values); + } + + @Test + void queryParamWithOptionalValue() { + UriComponents result = UriComponentsBuilder.newInstance() + .queryParam("foo", Optional.empty()) + .queryParam("baz", Optional.of("qux"), 42) + .build(); + + assertThat(result.getQuery()).isEqualTo("foo&baz=qux&baz=42"); + assertThat(result.getQueryParams()).containsOnlyKeys("foo", "baz") + .containsEntry("foo", Collections.singletonList(null)) + .containsEntry("baz", Arrays.asList("qux", "42")); } @Test