Support Optional in UriComponentsBuilder#queryParam

Closes gh-25951
This commit is contained in:
Rossen Stoyanchev
2020-10-23 18:08:54 +01:00
parent 5644a7aebb
commit e66e34766e
2 changed files with 27 additions and 7 deletions

View File

@@ -698,7 +698,7 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
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 class UriComponentsBuilder implements UriBuilder, Cloneable {
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()));