lukeautry / tsoa

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

tsc build error with file uploads #1642

Closed mattdean-digicatapult closed 2 months ago

mattdean-digicatapult commented 5 months ago

When using either @UploadedFile or @UploadedFiles to configure a form post to do a file upload the built routes.ts file contains invalid code.

Sorting

Expected Behavior

A handler with a file upload should stil compile to valid typescript which is compatible with multer type definitions:

  @Post('/')
  public async upload(
    @UploadedFile() file?: Express.Multer.File
  ): Promise<Attachment> { ... }

Current Behavior

The built routes.ts is not valid against the multer type definitions. An excess property multiple is populated on an object literal that is not defined on the multer.Multer.Field

Possible Solution

Remove this line https://github.com/lukeautry/tsoa/blame/d4a33015a7c0671cae8c24aae5afa3ea1fe44064/packages/cli/src/routeGeneration/routeGenerator.ts#L113 as it doesn't seem to add anything multer expects?

Steps to Reproduce

We've hit this on all our repos that do file upload. https://github.com/digicatapult/sqnc-matchmaker-api/pull/385 is one example.

You can also just create a handler as described in the expected behaviour section above

Context (Environment)

Version of the library: v6.3.1 Version of NodeJS: v20.x.x Version of Typescript: v5.4.5 Version of Multer: 1.4.5-lts.1

Detailed Description

When using either @UploadedFile or @UploadedFiles to configure a form post to do a file upload the built routes.ts contains in the request handler the code:

upload.fields([{"name":"file","maxCount":1,"multiple":false}]),

The problem with this is the type of multer.Multer.Field doesn't actually contain a property multiple see types package. Typescript therefore throw a build error:

src/routes.ts:1571:56 - error TS2353: Object literal may only specify known properties, and '"multiple"' does not exist in type 'Field'.

1571             upload.fields([{"name":"file","maxCount":1,"multiple":false}]),

I came across this during an upgrade to tsoa v6.3.0 but it also applied to the latest v6.3.1. This change was introduced in https://github.com/lukeautry/tsoa/pull/1620

Honestly maybe I'm missing something obvious as that field was clearly added in the above PR with intent. Looking at multer's source code however I cannot see anything that references a property multiple on a field.

Breaking change?

N/A

github-actions[bot] commented 5 months ago

Hello there mattdean-digicatapult 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

github-actions[bot] commented 4 months 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

mattdean-digicatapult commented 4 months ago

@WoH as far as I'm aware this is still an issue. Do you know if this has been fixed in a PR or if this was closed because of it going stale? If the latter can you please re-open the issue?

Also if my proposed fix is likely to be accepted I'm happy to submit a PR, I was just hoping someone from the project would comment that it sounded sensible.

sztyup commented 3 months ago

I also experience this issue

alexanderisacson commented 3 months ago

+1

I think we should reopen this issue.

vedtam commented 2 months ago

+1, same here

falk-stefan commented 1 month ago

+1 as of today