lukeautry / tsoa

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

tsc build error with file uploads #1642

Open mattdean-digicatapult opened 3 weeks ago

mattdean-digicatapult commented 3 weeks 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 3 weeks 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.👀