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

Helidon 4.x: Parameters.first(String) generates java.lang.IndexOutOfB… #8723

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -97,8 +97,9 @@ public String toString() {

@Override
public OptionalValue<String> first(String name) {
if (contains(name)) {
return OptionalValue.create(mapperManager, name, get(name), GenericType.STRING, qualifiers);
List<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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<String, List<String>> params = new HashMap<>();
params.put("param", List.of());
Parameters parameters = Parameters.create("test", params);
OptionalValue<String> value = parameters.first("param");
assertThat(value.isEmpty(), is(true));
}
}
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
16 changes: 15 additions & 1 deletion common/uri/src/test/java/io/helidon/common/uri/UriQueryTest.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 @@ -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<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));
}
}
}