lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.32k stars 481 forks source link

Using combination of @UploadedFile() and @UploadedFiles() decorators throws MulterError: Unexpected field #1619

Closed Yoronex closed 1 month ago

Yoronex commented 2 months ago

Sorting

Expected Behavior

The docs about file uploads suggest that it is possible to use a combination of @UploadedFiles() and @UploadedFile() in the example.

  @Post("uploadFile")
  public async uploadFile(
      @FormField() title: string,
      @FormField() description: string,
      @UploadedFiles() files: Express.Multer.File[],
      @UploadedFile() file: Express.Multer.File,
  ): Promise<void> {
    console.log(files);
  }

Current Behavior

A multer error is thrown and no HTTP response is sent to the client (in Swagger UI, the endpoint loads forever).

MulterError: Unexpected field
    at wrappedFileFilter (C:\Users\roy.kakkenberg\WebstormProjects\arvisan-backend\node_modules\multer\index.js:40:19)
    at Multipart.<anonymous> (C:\Users\roy.kakkenberg\WebstormProjects\arvisan-backend\node_modules\multer\lib\make-middleware.js:107:7)
    at Multipart.emit (node:events:514:28)
    at Multipart.emit (node:domain:488:12)
    at HeaderParser.cb (C:\Users\roy.kakkenberg\WebstormProjects\arvisan-backend\node_modules\busboy\lib\types\multipart.js:358:14)
    at HeaderParser.push (C:\Users\roy.kakkenberg\WebstormProjects\arvisan-backend\node_modules\busboy\lib\types\multipart.js:162:20)
    at SBMH.ssCb [as _cb] (C:\Users\roy.kakkenberg\WebstormProjects\arvisan-backend\node_modules\busboy\lib\types\multipart.js:394:37)
    at feed (C:\Users\roy.kakkenberg\WebstormProjects\arvisan-backend\node_modules\streamsearch\lib\sbmh.js:248:10)
    at SBMH.push (C:\Users\roy.kakkenberg\WebstormProjects\arvisan-backend\node_modules\streamsearch\lib\sbmh.js:104:16)
    at Multipart._write (C:\Users\roy.kakkenberg\WebstormProjects\arvisan-backend\node_modules\busboy\lib\types\multipart.js:567:19) {
  code: 'LIMIT_UNEXPECTED_FILE',
  field: 'files',
  storageErrors: []
}

Possible Solution

--

Steps to Reproduce

  1. Copy the example from the TSOA docs to upload files into a controller class.
  2. Try to upload two files (one for each name).
  3. Multer throws an error.

Context (Environment)

Version of the library: 6.2.1 Version of NodeJS: 20.9.0

Detailed Description

For my project, I wish to create and endpoint that takes multiple files as input. These files need to be organized, so I wish to use a combination of @UploadedFile() and @UploadedFiles() parameters for my endpoint. PR https://github.com/lukeautry/tsoa/pull/1541 implemented multiple files into separate fields, but the test case at https://github.com/lukeautry/tsoa/blob/1c4dfbf4e7dd8c3df716989d83c43caa9ff50559/tests/fixtures/controllers/postController.ts#L71 only tests multiple fields with UploadedFile() (the singular, not the plural). Indeed, if I use two fields of the singular version, the endpoint works fine.

Breaking change?

Yoronex commented 2 months ago

Implemented fix in https://github.com/lukeautry/tsoa/pull/1620.

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

Yoronex commented 1 month ago

Is there anything more I can do get this PR approved?