Polishing contribution
Closes gh-25951
This commit is contained in:
@@ -336,14 +336,20 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DefaultUriBuilder queryParamIfPresent(String name, Optional<?> optionalValue) {
|
public DefaultUriBuilder queryParam(String name, @Nullable Collection<?> values) {
|
||||||
this.uriComponentsBuilder.queryParamIfPresent(name, optionalValue);
|
this.uriComponentsBuilder.queryParam(name, values);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DefaultUriBuilder queryParam(String name, @Nullable Collection<?> values) {
|
public DefaultUriBuilder queryParamIfPresent(String name, Optional<?> value) {
|
||||||
this.uriComponentsBuilder.queryParam(name, values);
|
this.uriComponentsBuilder.queryParamIfPresent(name, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DefaultUriBuilder queryParams(MultiValueMap<String, String> params) {
|
||||||
|
this.uriComponentsBuilder.queryParams(params);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -359,12 +365,6 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public DefaultUriBuilder queryParams(MultiValueMap<String, String> params) {
|
|
||||||
this.uriComponentsBuilder.queryParams(params);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DefaultUriBuilder replaceQueryParams(MultiValueMap<String, String> params) {
|
public DefaultUriBuilder replaceQueryParams(MultiValueMap<String, String> params) {
|
||||||
this.uriComponentsBuilder.replaceQueryParams(params);
|
this.uriComponentsBuilder.replaceQueryParams(params);
|
||||||
|
|||||||
@@ -186,15 +186,6 @@ public interface UriBuilder {
|
|||||||
*/
|
*/
|
||||||
UriBuilder queryParam(String name, Object... values);
|
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.
|
* Variant of {@link #queryParam(String, Object...)} with a Collection.
|
||||||
* <p><strong>Note: </strong> please, review the Javadoc of
|
* <p><strong>Note: </strong> please, review the Javadoc of
|
||||||
@@ -207,6 +198,16 @@ public interface UriBuilder {
|
|||||||
*/
|
*/
|
||||||
UriBuilder queryParam(String name, @Nullable Collection<?> values);
|
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.
|
* Add multiple query parameters and values.
|
||||||
* <p><strong>Note: </strong> please, review the Javadoc of
|
* <p><strong>Note: </strong> please, review the Javadoc of
|
||||||
|
|||||||
@@ -710,22 +710,21 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UriComponentsBuilder queryParamIfPresent(String name, Optional<?> optionalValue) {
|
public UriComponentsBuilder queryParam(String name, @Nullable Collection<?> values) {
|
||||||
if (optionalValue.isPresent()) {
|
return queryParam(name, (CollectionUtils.isEmpty(values) ? EMPTY_VALUES : values.toArray()));
|
||||||
Object value = optionalValue.get();
|
|
||||||
if (value instanceof Collection) {
|
|
||||||
queryParam(name, (Collection) value);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
queryParam(name, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UriComponentsBuilder queryParam(String name, @Nullable Collection<?> values) {
|
public UriComponentsBuilder queryParamIfPresent(String name, Optional<?> value) {
|
||||||
return queryParam(name, (CollectionUtils.isEmpty(values) ? EMPTY_VALUES : values.toArray()));
|
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.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -206,33 +206,6 @@ class UriComponentsBuilderTests {
|
|||||||
assertThat(uri.toString()).isEqualTo(httpUrl);
|
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
|
@Test // SPR-10539
|
||||||
void fromUriStringIPv6Host() {
|
void fromUriStringIPv6Host() {
|
||||||
UriComponents result = UriComponentsBuilder
|
UriComponents result = UriComponentsBuilder
|
||||||
@@ -786,6 +759,30 @@ class UriComponentsBuilderTests {
|
|||||||
assertThat(result.getQueryParams()).isEqualTo(expectedQueryParams);
|
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
|
@Test
|
||||||
void emptyQueryParam() {
|
void emptyQueryParam() {
|
||||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||||
|
|||||||
Reference in New Issue
Block a user