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 cfef61c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 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(""));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -93,6 +93,7 @@ public void testFormContent() {
assertThat(received.all(SPECIAL, List::of), is(ADVANCED_TEST_FORM.all(SPECIAL)));
assertThat(received.all(MULTIPLE), is(ADVANCED_TEST_FORM.all(MULTIPLE)));
assertThat(received.all(NO_VALUE).size(), is(0));
assertThat(received.first(NO_VALUE).isEmpty(), is(true));
}
}
}

0 comments on commit cfef61c

Please sign in to comment.