Skip to content
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

Support Optional for query parameters in UriBuilder and UriComponentsBuilder #25951

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 @@ -21,6 +21,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;
Expand Down Expand Up @@ -334,6 +335,12 @@ public DefaultUriBuilder queryParam(String name, Object... values) {
return this;
}

@Override
public DefaultUriBuilder queryParamIfPresent(String name, Optional<?> optionalValue) {
this.uriComponentsBuilder.queryParamIfPresent(name, optionalValue);
return this;
}

@Override
public DefaultUriBuilder queryParam(String name, @Nullable Collection<?> values) {
this.uriComponentsBuilder.queryParam(name, values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.URI;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;
Expand Down Expand Up @@ -185,6 +186,15 @@ 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
*/
rstoyanchev marked this conversation as resolved.
Show resolved Hide resolved
UriBuilder queryParamIfPresent(String name, Optional<?> optionalValue);

/**
* Variant of {@link #queryParam(String, Object...)} with a Collection.
* <p><strong>Note: </strong> please, review the Javadoc of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -708,6 +709,20 @@ public UriComponentsBuilder queryParam(String name, Object... values) {
return this;
}

@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;
}

@Override
public UriComponentsBuilder queryParam(String name, @Nullable Collection<?> values) {
return queryParam(name, (CollectionUtils.isEmpty(values) ? EMPTY_VALUES : values.toArray()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

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.Map;
import java.util.Optional;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -204,6 +206,33 @@ void fromUriStringQueryParamEncodedAndContainingPlus() {
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
Expand Down