Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Yoronex
Copy link

@Yoronex Yoronex commented Apr 29, 2024

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you written unit tests?
  • Have you written unit tests that cover the negative cases (i.e.: if bad data is submitted, does the library respond properly)?
  • This PR is associated with an existing issue?

Closing issues

Closes #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().

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello there Yoronex 👋

Thank you and congrats 🎉 for opening your first PR on this project.✨

We will review the following PR soon! 👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using combination of @UploadedFile() and @UploadedFiles() decorators throws MulterError: Unexpected field
1 participant