Skip to content

Commit

Permalink
Merge pull request #954 from apache/fix/WW-5423-query-params
Browse files Browse the repository at this point in the history
[WW-5423] Fixes returning null instead of empty array in case of non-existing param
  • Loading branch information
lukaszlenart authored Jun 10, 2024
2 parents baab7dd + 5015ea0 commit 62eac8f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,11 @@ public Enumeration<String> getParameterNames() {
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameterValues(java.lang.String)
*/
public String[] getParameterValues(String name) {
return parameters.getOrDefault(name, Collections.emptyList())
.toArray(String[]::new);
List<String> values = parameters.get(name);
if (values == null) {
return null;
}
return values.toArray(new String[0]);
}

/* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public interface MultiPartRequest {
/**
* Returns a list of all parameter values associated with a parameter name. If there is only
* one parameter value per name the resulting array will be of length 1.
* If the parameter doesn't exist, null should be returned instead of empty array.
*
* @param name the name of the parameter.
* @return an array of all values associated with the parameter name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void uploadedFilesToDisk() throws IOException {
.isEmpty();

assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
.asList()
.asInstanceOf(InstanceOfAssertFactories.LIST)
.containsOnly("file1", "file2");
assertThat(multiPart.getFile("file1")).allSatisfy(file -> {
assertThat(file.isFile())
Expand Down Expand Up @@ -142,7 +142,7 @@ public void uploadedMultipleFilesToDisk() throws IOException {
.isEmpty();

assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
.asList()
.asInstanceOf(InstanceOfAssertFactories.LIST)
.containsOnly("file1");
assertThat(multiPart.getFile("file1")).allSatisfy(file -> {
if (Objects.equals(file.getName(), "test1.csv")) {
Expand Down Expand Up @@ -193,7 +193,7 @@ public void uploadedFilesWithLargeBuffer() throws IOException {
.isEmpty();

assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
.asList()
.asInstanceOf(InstanceOfAssertFactories.LIST)
.containsOnly("file1", "file2");
assertThat(multiPart.getFile("file1")).allSatisfy(file -> {
assertThat(file.isFile())
Expand Down Expand Up @@ -240,7 +240,7 @@ public void cleanUp() throws IOException {
.isEmpty();

assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
.asList()
.asInstanceOf(InstanceOfAssertFactories.LIST)
.containsOnly("file1", "file2");
assertThat(multiPart.getFile("file1")).allSatisfy(file -> {
assertThat(file.isFile())
Expand Down Expand Up @@ -306,7 +306,7 @@ public void nonMultiPartUpload() throws IOException {
.containsExactly("struts.messages.upload.error.FileUploadContentTypeException");

assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
.asList()
.asInstanceOf(InstanceOfAssertFactories.LIST)
.isEmpty();
}

Expand Down Expand Up @@ -410,7 +410,7 @@ public void mismatchCharset() throws IOException {
.isEmpty();

assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
.asList()
.asInstanceOf(InstanceOfAssertFactories.LIST)
.containsOnly("file1");
assertThat(multiPart.getFile("file1")).allSatisfy(file -> {
assertThat(file.isFile())
Expand Down Expand Up @@ -458,6 +458,8 @@ public void normalFields() throws IOException {
.isEqualTo("short text");
assertThat(multiPart.getParameterValues("multi"))
.containsOnly("multi1", "multi2");
assertThat(multiPart.getParameterValues("not-existing"))
.isNull();
}

@Test
Expand Down

0 comments on commit 62eac8f

Please sign in to comment.