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 1 commit
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 optionalQueryParam(String name, Optional<?> optionalValue) {
this.uriComponentsBuilder.optionalQueryParam(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);

/**
* Append the given query parameter and value if and only if a value is present in the Optional.
* No action will be taken, and the query parameter name will not be added, if the passed Optional 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 optionalQueryParam(String name, Optional<?> optionalValue);
rstoyanchev marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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,14 @@ public UriComponentsBuilder queryParam(String name, Object... values) {
return this;
}

@Override
public UriComponentsBuilder optionalQueryParam(String name, Optional<?> optionalValue) {
if (optionalValue.isPresent()) {
queryParam(name, optionalValue.get());
}
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 @@ -23,6 +23,7 @@
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 +205,18 @@ void fromUriStringQueryParamEncodedAndContainingPlus() {
assertThat(uri.toString()).isEqualTo(httpUrl);
}


@Test
void queryParamAsOptional() {
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
UriComponents result = builder.optionalQueryParam("baz", Optional.of("qux")).optionalQueryParam("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 // SPR-10539
void fromUriStringIPv6Host() {
UriComponents result = UriComponentsBuilder
Expand Down