swagger: CLI plugin does not with SWC

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Currently, the CLI plugin generates an invalid metadata.ts file. It uses invalid import paths on Windows, includes syntax errors with the summary field for @ApiOperation, and also generates missing imports for enums or classes which are used in the DTO but not exported (which it doesn’t have any issues with when using TSC).

  • broken paths on windows image
  • Linux paths (correctly relative to the src directory) image
  • invalid summary field for endpoints it’s worth noting that removing the @ApiOperation tag or disabling the introspectComments option avoids this bug. image image
  • missing imports for non-exported enums/classes used in exported DTOs, when exported they are correctly referenced and imported in the t types array. image

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-e46jbt?file=src%2Fmetadata.ts

Steps to reproduce

Note SWC doesn’t work on Stackblitz at the moment so you may want to download it locally, the type-checking does work and the error is present on Stackblitz. Obviously, you’d need a Windows machine for the incorrect path bug.

  1. create a nestjs project with swc and swagger
  2. create a dto and use the a dto
  3. annotate an endpoint with the dto, description and summary.
  4. run nest build -b tsc --type-check

Expected behavior

The above errors should not occur, --type-check should be compatible with a codebase that worked with TSC, if not, we should mention how to make it compatible in the docs.

Package version

7.0.4

NestJS version

10.0.3

Node.js version

v18.13.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 23 (9 by maintainers)

Most upvoted comments

I don’t know, maybe i should create a new bug request, but, this bug relevant to this topic.

The thing in PartialType. It doesn’t work with SWC compiler. Like: export class UpdateEquipmentDTO extends PartialType(CreateEquipmentDTO) {}

Made output like that: metadata.ts models: [ [ import("./equipments/DTO/create-equipment.dto"), { CreateSectionDTO: { title: { required: true, type: () => String, minLength: 1 }, imageURL: { required: false, type: () => String }, }, CreateEquipmentDTO: { title: { required: true, type: () => String, minLength: 1 }, shortDescription: { required: false, type: () => String, maxLength: 300, }, description: { required: false, type: () => String, maxLength: 5000, }, price: { required: true, type: () => Number }, imageURLs: { required: true, type: () => [String] }, previewImageURL: { required: false, type: () => String }, sectionId: { required: true, type: () => Number }, }, }, ], [ import("./equipments/DTO/update-equipment.dto"), { UpdateEquipmentDTO: {} }, ], ...

Or screenshot: image

DISCORD TOPIC: https://discord.com/channels/520622812742811698/1126116119783161897

Just updated the docs to use await, great catch!

One last thing I wanted to check, which I mentioned briefly before but the build command does not wait for the metadata.ts file to be generated and will fail unless one exists

Build command doesn’t wait to run SWC only in the --watch mode which shouldn’t be used in CIs anyway. It does perform type-checking earlier though, as type-checking is actually required for us to produce the metadata file.

I think that also means the metadata.ts file it uses could be out of date and will break CI’s unless it’s checked into version control.

As long as this file exists, it can be safely out-of-the-date as it will be updated in CI anyway before the application is launched.

We might need to add more info on that to the docs, great questions!