lukeautry / tsoa

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

Fix Multer error when @UploadedFiles() is used twice in a route #1620

Closed Yoronex closed 1 month ago

Yoronex commented 2 months ago

All Submissions:

Closing issues

Closes https://github.com/lukeautry/tsoa/issues/1619.

Potential Problems With The Approach

There are not that many files present in the test folder to use for testing uploading files. Right now the same file is included multiple times, but the order is always different, so this should not matter.

The routeGenerator.ts now also adds two extra attributes to each file upload field, namely maxCount and multiple. The maxCount attribute is used by Multer to enforce only one file is uploaded to that field. However, HandleBars aims to be logic free, so a simple check (maxCount == 1) cannot be done in hapi.hbs to determine whether multiple files can be uploaded or not. Therefore, the multiple attribute is also added, but by design of the json function in Handlebars, this extra variable is also included in the Express and Koa routes.ts. Luckily, it does not do anything, but it is not very elegant. If there is a better way to do this, I would love to hear it!

Test plan

The unit tests that I added includes the case mentioned in the issue, but also two @UploadedFiles() statement to truly verify that these arrays are separated correctly.

The fix simply removes the upload.array() lines from any routes.ts files, because the same can be achieved by using upload.fields().

WoH commented 1 month ago

Sorry for the long delay, lgtm

douglasg14b commented 1 week ago

Might be a regression here? https://github.com/lukeautry/tsoa/issues/1642