Polishing contribution
Closes gh-25951
This commit is contained in:
@@ -336,14 +336,20 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultUriBuilder queryParamIfPresent(String name, Optional<?> optionalValue) {
|
||||
this.uriComponentsBuilder.queryParamIfPresent(name, optionalValue);
|
||||
public DefaultUriBuilder queryParam(String name, @Nullable Collection<?> values) {
|
||||
this.uriComponentsBuilder.queryParam(name, values);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultUriBuilder queryParam(String name, @Nullable Collection<?> values) {
|
||||
this.uriComponentsBuilder.queryParam(name, values);
|
||||
public DefaultUriBuilder queryParamIfPresent(String name, Optional<?> value) {
|
||||
this.uriComponentsBuilder.queryParamIfPresent(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultUriBuilder queryParams(MultiValueMap<String, String> params) {
|
||||
this.uriComponentsBuilder.queryParams(params);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -359,12 +365,6 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultUriBuilder queryParams(MultiValueMap<String, String> params) {
|
||||
this.uriComponentsBuilder.queryParams(params);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultUriBuilder replaceQueryParams(MultiValueMap<String, String> params) {
|
||||
this.uriComponentsBuilder.replaceQueryParams(params);
|
||||
|
||||
@@ -186,15 +186,6 @@ public interface UriBuilder {
|
||||
*/
|
||||
UriBuilder queryParam(String name, Object... values);
|
||||
|
||||
/**
|
||||
* Delegates to {@link #queryParam(String, Object...)} or {@link #queryParam(String, Object...)} if and only if optionalValue has a value.
|
||||
* No action will be taken, and the query parameter name will not be added, if optionalValue is empty.
|
||||
* @param name the query parameter name
|
||||
* @param optionalValue an Optional, either empty or holding the query parameter value.
|
||||
* @return
|
||||
*/
|
||||
UriBuilder queryParamIfPresent(String name, Optional<?> optionalValue);
|
||||
|
||||
/**
|
||||
* Variant of {@link #queryParam(String, Object...)} with a Collection.
|
||||
* <p><strong>Note: </strong> please, review the Javadoc of
|
||||
@@ -207,6 +198,16 @@ public interface UriBuilder {
|
||||
*/
|
||||
UriBuilder queryParam(String name, @Nullable Collection<?> values);
|
||||
|
||||
/**
|
||||
* Delegates to either {@link #queryParam(String, Object...)} or
|
||||
* {@link #queryParam(String, Collection)} if the given {@link Optional} has
|
||||
* a value, or else if it is empty, no query parameter is added at all.
|
||||
* @param name the query parameter name
|
||||
* @param value an Optional, either empty or holding the query parameter value.
|
||||
* @since 5.3
|
||||
*/
|
||||
UriBuilder queryParamIfPresent(String name, Optional<?> value);
|
||||
|
||||
/**
|
||||
* Add multiple query parameters and values.
|
||||
* <p><strong>Note: </strong> please, review the Javadoc of
|
||||
|
||||
@@ -710,22 +710,21 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public UriComponentsBuilder queryParamIfPresent(String name, Optional<?> optionalValue) {
|
||||
if (optionalValue.isPresent()) {
|
||||
Object value = optionalValue.get();
|
||||
if (value instanceof Collection) {
|
||||
queryParam(name, (Collection) value);
|
||||
}
|
||||
else {
|
||||
queryParam(name, value);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
public UriComponentsBuilder queryParam(String name, @Nullable Collection<?> values) {
|
||||
return queryParam(name, (CollectionUtils.isEmpty(values) ? EMPTY_VALUES : values.toArray()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public UriComponentsBuilder queryParam(String name, @Nullable Collection<?> values) {
|
||||
return queryParam(name, (CollectionUtils.isEmpty(values) ? EMPTY_VALUES : values.toArray()));
|
||||
public UriComponentsBuilder queryParamIfPresent(String name, Optional<?> value) {
|
||||
value.ifPresent(o -> {
|
||||
if (o instanceof Collection) {
|
||||
queryParam(name, (Collection<?>) o);
|
||||
}
|
||||
else {
|
||||
queryParam(name, o);
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,11 +18,11 @@ package org.springframework.web.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -206,33 +206,6 @@ class UriComponentsBuilderTests {
|
||||
assertThat(uri.toString()).isEqualTo(httpUrl);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void queryParamIfPresent() {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
UriComponents result = builder.queryParamIfPresent("baz", Optional.of("qux")).queryParamIfPresent("foo", Optional.empty()).build();
|
||||
|
||||
assertThat(result.getQuery()).isEqualTo("baz=qux");
|
||||
MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<>(1);
|
||||
expectedQueryParams.add("baz", "qux");
|
||||
assertThat(result.getQueryParams()).isEqualTo(expectedQueryParams);
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryParamIfPresentCollection() {
|
||||
Collection<String> c = new ArrayList<>();
|
||||
c.add("foo");
|
||||
c.add("bar");
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
UriComponents result = builder.queryParamIfPresent("baz", Optional.of(c)).build();
|
||||
|
||||
assertThat(result.getQuery()).isEqualTo("baz=foo&baz=bar");
|
||||
MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<>(1);
|
||||
expectedQueryParams.add("baz", "foo");
|
||||
expectedQueryParams.add("baz", "bar");
|
||||
assertThat(result.getQueryParams()).isEqualTo(expectedQueryParams);
|
||||
}
|
||||
|
||||
@Test // SPR-10539
|
||||
void fromUriStringIPv6Host() {
|
||||
UriComponents result = UriComponentsBuilder
|
||||
@@ -786,6 +759,30 @@ class UriComponentsBuilderTests {
|
||||
assertThat(result.getQueryParams()).isEqualTo(expectedQueryParams);
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryParamIfPresent() {
|
||||
UriComponents result = UriComponentsBuilder.newInstance()
|
||||
.queryParamIfPresent("baz", Optional.of("qux"))
|
||||
.queryParamIfPresent("foo", Optional.empty())
|
||||
.build();
|
||||
|
||||
assertThat(result.getQuery()).isEqualTo("baz=qux");
|
||||
assertThat(result.getQueryParams())
|
||||
.containsOnlyKeys("baz")
|
||||
.containsEntry("baz", Collections.singletonList("qux"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryParamIfPresentCollection() {
|
||||
List<String> values = Arrays.asList("foo", "bar");
|
||||
UriComponents result = UriComponentsBuilder.newInstance()
|
||||
.queryParamIfPresent("baz", Optional.of(values))
|
||||
.build();
|
||||
|
||||
assertThat(result.getQuery()).isEqualTo("baz=foo&baz=bar");
|
||||
assertThat(result.getQueryParams()).containsOnlyKeys("baz").containsEntry("baz", values);
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyQueryParam() {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
|
||||
Reference in New Issue
Block a user