Skip to content

Allow multiple files with same name in form data #6419

@triberraar

Description

@triberraar

Description

When posting multipart form data it is possible to have the same field repeated. This is possible for both data fields and file fields.
For example (binary data removed to make the example simpler)

------WebKitFormBoundaryT3nTM6Bt4fg1LuwI
Content-Disposition: form-data; name="something"

2
------WebKitFormBoundaryT3nTM6Bt4fg1LuwI
Content-Disposition: form-data; name="something"

8
------WebKitFormBoundaryT3nTM6Bt4fg1LuwI
Content-Disposition: form-data; name="file"; filename="10.png"
Content-Type: image/png
------WebKitFormBoundaryT3nTM6Bt4fg1LuwI
Content-Disposition: form-data; name="file"; filename="11.png"
Content-Type: image/png

This does not work correctly when using annotation based endpoints in armeria>

  • @Param("something") Set<Integer> something => this works correctly and gets a list with 2 items
  • @Param("file") List<MultipartFile> file => this does not work and gets an exception with message Cannot resolve a value from a query parameter: file
  • @Param("file") List<MultipartFile> file => this works but only gets 1 of the files

When inspecting/debugging the bodyparts we can see that all files are available.
See p

@Put("")
@Consumes(MediaTypeNames.MULTIPART_FORM_DATA)
public HttpResponse debugMultipart(Multipart multipart) {
    return HttpResponse.of(multipart.aggregate().handle((aggregated, cause) -> {
        if (cause != null) {
            // do somethig;
            return HttpResponse.of(400);
        }
        final Builder<AggregatedBodyPart> files = ImmutableList.builder();
        final Builder<AggregatedBodyPart> nonFiles = ImmutableList.builder();
        for (AggregatedBodyPart bodyPart : aggregated.bodyParts()) {
            if (bodyPart.filename() != null) {
                files.add(bodyPart);
            } else {
                nonFiles.add(bodyPart);
            }
        }

        ///
        return HttpResponse.of(200);
    }));
}

In this we can see that files has 2 items with the correct file names.

Solution

Make it so that a list of MultipartFile or Path can be used in the @param bindings when using annotation based controllers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions