Skip to content

Commit

Permalink
First returns empty optional when list is empty
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed May 6, 2024
1 parent 55e54d1 commit 85b857c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ParametersTest {
@Test
void testEmpty() {
Parameters empty = Parameters.empty(UNIT_TEST);
assertThat(empty.first(UNIT_TEST).isEmpty(), is(true));
assertThat(empty.component(), is(UNIT_TEST));
assertThat("Empty parameters should not contain anything", empty.contains("anything"), is(false));
assertThat("Empty parameters should have size 0", empty.size(), is(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ public String get(String name) throws NoSuchElementException {
public OptionalValue<String> first(String name) {
ensureDecoded();
List<String> values = decodedQueryParams.get(name);
if (values == null) {
if (values == null || values.isEmpty()) {
return OptionalValue.create(MapperManager.global(), name, GenericType.STRING, "uri", "query");
}
String value = values.isEmpty() ? "" : values.iterator().next();
return OptionalValue.create(MapperManager.global(), name, value, GenericType.STRING, "uri", "query");
return OptionalValue.create(MapperManager.global(), name, values.iterator().next(),
GenericType.STRING, "uri", "query");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.net.URI;
import java.net.URLEncoder;

import io.helidon.common.mapper.OptionalValue;

import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.US_ASCII;
Expand Down Expand Up @@ -76,8 +78,12 @@ void testNullUriFails() {
@Test
void issue8710() {
UriQuery uriQuery = UriQuery.create(URI.create("http://foo/bar?a&b=c"));

OptionalValue<String> optional = uriQuery.first("a");
assertThat(optional.isEmpty(), is(true));

assertThat(uriQuery.all("a"), hasItems());
assertThat(uriQuery.all("b"), hasItems("c"));
assertThat(uriQuery.getRaw("a"), is(""));
}

}

0 comments on commit 85b857c

Please sign in to comment.