nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:
https://nestjs.com
MIT License
1.68k stars 469 forks source link

CLI plugin not working (possibly due to Turborepo?) #2640

Closed benjidotsh closed 1 year ago

benjidotsh commented 1 year ago

Is there an existing issue for this?

Current behavior

I'm trying to get the Swagger CLI plugin working with a NestJS project that is part of a Turborepo project, but it doesn't seem to work.

The schemas remain empty, even though they have a property. I also checked the built DTO file, but I could not find a _OPENAPI_METADATA_FACTORY.

I tried reproducing this with a new NestJS project, which I couldn't, so I feel like this might be related to the usage of Turborepo.

Minimum reproduction code

https://github.com/icapps/NestKit/tree/swagger

Steps to reproduce

  1. Clone the repository
  2. Run npm install
  3. Run npx prisma generate in the example folder
  4. Copy and rename the .env.example file to .env
  5. Run npm run dev
  6. Go to http://localhost:3000/swagger and/or take a look at example/dist/todos/dto/create-todo.dto.js

Expected behavior

The schemas should be automatically generated and example/dist/todos/dto/create-todo.dto.js should contain an _OPENAPI_METADATA_FACTORY.

Package version

7.1.12

NestJS version

10.0.0

Node.js version

20.7.0

In which operating systems have you tested?

Other

No response

jmcdo29 commented 1 year ago

I could be wrong here, but from a cursory look, your Nest application makes use of the common and prisma packages through a workspace protocol. The common and prisma have separate build steps from the nest build/nest start --watch commands, and so when nest start --watch is ran, it sees that there's js packages for the common package, and it doesn't need to be compiled. Because of this, there's no swagger plugin to be ran, as there's no Typescript metadata to make use of, so when you use the type at runtime, it uses the JS type that was created by tsup, not the nest compiler with the swagger plugin, and so you have no output data on the swagger ui.

To actually have the swagger plugin work, you'd need to compile the common project using @nestjs/cli so that the swagger plugin can read the Typescript type data, and provide the correct _OPENAPI_METADATA_FACTORY to the transpiled type

kamilmysliwiec commented 1 year ago

Please, use our Discord channel (support) for further questions. We are using GitHub to track bugs, feature requests, and potential improvements.