diff --git a/common/parameters/src/main/java/io/helidon/common/parameters/ParametersMap.java b/common/parameters/src/main/java/io/helidon/common/parameters/ParametersMap.java index 55c82e4905b..a7f24959d8d 100644 --- a/common/parameters/src/main/java/io/helidon/common/parameters/ParametersMap.java +++ b/common/parameters/src/main/java/io/helidon/common/parameters/ParametersMap.java @@ -97,8 +97,9 @@ public String toString() { @Override public OptionalValue first(String name) { - if (contains(name)) { - return OptionalValue.create(mapperManager, name, get(name), GenericType.STRING, qualifiers); + List value = params.get(name); + if (value != null && !value.isEmpty()) { + return OptionalValue.create(mapperManager, name, value.get(0), GenericType.STRING, qualifiers); } return OptionalValue.create(mapperManager, name, GenericType.STRING, qualifiers); } diff --git a/common/parameters/src/test/java/io/helidon/common/parameters/ParametersTest.java b/common/parameters/src/test/java/io/helidon/common/parameters/ParametersTest.java index 27aa1204abd..4b7cc5111b0 100644 --- a/common/parameters/src/test/java/io/helidon/common/parameters/ParametersTest.java +++ b/common/parameters/src/test/java/io/helidon/common/parameters/ParametersTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2023 Oracle and/or its affiliates. + * Copyright (c) 2022, 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. @@ -16,10 +16,13 @@ package io.helidon.common.parameters; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import io.helidon.common.mapper.OptionalValue; + import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.containsString; @@ -35,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)); @@ -111,4 +115,13 @@ void testBuilder() { assertThat(params.toString(), containsString(UNIT_TEST)); assertThat("Parameters should not be empty", params.isEmpty(), is(false)); } + + @Test + void issue8710() { + Map> params = new HashMap<>(); + params.put("param", List.of()); + Parameters parameters = Parameters.create("test", params); + OptionalValue value = parameters.first("param"); + assertThat(value.isEmpty(), is(true)); + } } \ No newline at end of file diff --git a/common/uri/src/main/java/io/helidon/common/uri/UriQueryImpl.java b/common/uri/src/main/java/io/helidon/common/uri/UriQueryImpl.java index 526efa5b61c..dbe14f22650 100644 --- a/common/uri/src/main/java/io/helidon/common/uri/UriQueryImpl.java +++ b/common/uri/src/main/java/io/helidon/common/uri/UriQueryImpl.java @@ -148,11 +148,11 @@ public String get(String name) throws NoSuchElementException { public OptionalValue first(String name) { ensureDecoded(); List 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 diff --git a/common/uri/src/test/java/io/helidon/common/uri/UriQueryTest.java b/common/uri/src/test/java/io/helidon/common/uri/UriQueryTest.java index d96c154f4d7..edb01dd62bd 100644 --- a/common/uri/src/test/java/io/helidon/common/uri/UriQueryTest.java +++ b/common/uri/src/test/java/io/helidon/common/uri/UriQueryTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2023 Oracle and/or its affiliates. + * Copyright (c) 2022, 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. @@ -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; @@ -72,4 +74,16 @@ void testNullQueryFails() { void testNullUriFails() { assertThrows(NullPointerException.class, () -> UriQuery.create((URI) null)); } + + @Test + void issue8710() { + UriQuery uriQuery = UriQuery.create(URI.create("http://foo/bar?a&b=c")); + OptionalValue 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("")); + } + } \ No newline at end of file diff --git a/webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/FormTest.java b/webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/FormTest.java index 94dc241cd84..abb906cc0b6 100644 --- a/webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/FormTest.java +++ b/webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/FormTest.java @@ -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. @@ -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)); } } }