Skip to content

Commit 5015ea0

Browse files
committed
WW-5423 Fixes returning null instead of empty array in case of non-existing param
1 parent baab7dd commit 5015ea0

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

core/src/main/java/org/apache/struts2/dispatcher/multipart/AbstractMultiPartRequest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,11 @@ public Enumeration<String> getParameterNames() {
372372
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameterValues(java.lang.String)
373373
*/
374374
public String[] getParameterValues(String name) {
375-
return parameters.getOrDefault(name, Collections.emptyList())
376-
.toArray(String[]::new);
375+
List<String> values = parameters.get(name);
376+
if (values == null) {
377+
return null;
378+
}
379+
return values.toArray(new String[0]);
377380
}
378381

379382
/* (non-Javadoc)

core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public interface MultiPartRequest {
9595
/**
9696
* Returns a list of all parameter values associated with a parameter name. If there is only
9797
* one parameter value per name the resulting array will be of length 1.
98+
* If the parameter doesn't exist, null should be returned instead of empty array.
9899
*
99100
* @param name the name of the parameter.
100101
* @return an array of all values associated with the parameter name.

core/src/test/java/org/apache/struts2/dispatcher/multipart/AbstractMultiPartRequestTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void uploadedFilesToDisk() throws IOException {
9393
.isEmpty();
9494

9595
assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
96-
.asList()
96+
.asInstanceOf(InstanceOfAssertFactories.LIST)
9797
.containsOnly("file1", "file2");
9898
assertThat(multiPart.getFile("file1")).allSatisfy(file -> {
9999
assertThat(file.isFile())
@@ -142,7 +142,7 @@ public void uploadedMultipleFilesToDisk() throws IOException {
142142
.isEmpty();
143143

144144
assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
145-
.asList()
145+
.asInstanceOf(InstanceOfAssertFactories.LIST)
146146
.containsOnly("file1");
147147
assertThat(multiPart.getFile("file1")).allSatisfy(file -> {
148148
if (Objects.equals(file.getName(), "test1.csv")) {
@@ -193,7 +193,7 @@ public void uploadedFilesWithLargeBuffer() throws IOException {
193193
.isEmpty();
194194

195195
assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
196-
.asList()
196+
.asInstanceOf(InstanceOfAssertFactories.LIST)
197197
.containsOnly("file1", "file2");
198198
assertThat(multiPart.getFile("file1")).allSatisfy(file -> {
199199
assertThat(file.isFile())
@@ -240,7 +240,7 @@ public void cleanUp() throws IOException {
240240
.isEmpty();
241241

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

308308
assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
309-
.asList()
309+
.asInstanceOf(InstanceOfAssertFactories.LIST)
310310
.isEmpty();
311311
}
312312

@@ -410,7 +410,7 @@ public void mismatchCharset() throws IOException {
410410
.isEmpty();
411411

412412
assertThat(multiPart.getFileParameterNames().asIterator()).toIterable()
413-
.asList()
413+
.asInstanceOf(InstanceOfAssertFactories.LIST)
414414
.containsOnly("file1");
415415
assertThat(multiPart.getFile("file1")).allSatisfy(file -> {
416416
assertThat(file.isFile())
@@ -458,6 +458,8 @@ public void normalFields() throws IOException {
458458
.isEqualTo("short text");
459459
assertThat(multiPart.getParameterValues("multi"))
460460
.containsOnly("multi1", "multi2");
461+
assertThat(multiPart.getParameterValues("not-existing"))
462+
.isNull();
461463
}
462464

463465
@Test

0 commit comments

Comments
 (0)