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

NestJS Swagger Plugin when used with SWC references undeclared variable. #12459

Closed
3 of 15 tasks
evanwhitten opened this issue Sep 26, 2023 · 1 comment
Closed
3 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@evanwhitten
Copy link

evanwhitten commented Sep 26, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When @nestjs/swagger CLI plugin is used alongside SWC, it generates output in the metadata.ts from the PluginMetadataGenerator that includes undeclared variables. These variables inside the metadata.ts trigger failures when type checking, also during runtime if the metadata is attempted to be used somewhere like loadPluginMetadata.

In my reproduction I have HelloClass and HelloClassSeperate demonstrating that this bug is only present when the class exists in the same file as the DTO.

✔  TSC  Initializing type checker...
>  TSC  Found 0 issues. Generating metadata...
>  SWC  Running...
Successfully compiled: 8 files with swc (7.99ms)
[Nest] 3860  - 09/26/2023, 4:46:59 PM     LOG [NestFactory] Starting Nest application...
[Nest] 3860  - 09/26/2023, 4:46:59 PM     LOG [InstanceLoader] AppModule dependencies initialized +6ms

/Users/evanwhitten/Git/reproduce-cli-swagger-reference/dist/metadata.js:76
                                        HelloClass
                                        ^
ReferenceError: HelloClass is not defined
    at Object.type (/Users/evanwhitten/Git/reproduce-cli-swagger-reference/dist/metadata.js:76:41)
    at SchemaObjectFactory.mergePropertyWithMetadata (/Users/evanwhitten/Git/reproduce-cli-swagger-reference/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:127:38)
    at /Users/evanwhitten/Git/reproduce-cli-swagger-reference/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:82:35
    at Array.map (<anonymous>)
    at SchemaObjectFactory.extractPropertiesFromType (/Users/evanwhitten/Git/reproduce-cli-swagger-reference/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:81:52)
    at SchemaObjectFactory.exploreModelSchema (/Users/evanwhitten/Git/reproduce-cli-swagger-reference/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:103:41)
    at ResponseObjectFactory.create (/Users/evanwhitten/Git/reproduce-cli-swagger-reference/node_modules/@nestjs/swagger/dist/services/response-object-factory.js:46:47)
    at /Users/evanwhitten/Git/reproduce-cli-swagger-reference/node_modules/@nestjs/swagger/dist/explorers/api-response.explorer.js:67:101
    at /Users/evanwhitten/Git/reproduce-cli-swagger-reference/node_modules/lodash/lodash.js:13469:38
    at /Users/evanwhitten/Git/reproduce-cli-swagger-reference/node_modules/lodash/lodash.js:4967:15

metadata.ts -> Problem area: { required: true, type: () => [HelloClass] }

export default async () => {
    const t = {
        ["./seperate.dto"]: await import("./seperate.dto"),
        ["./app.dto"]: await import("./app.dto")
    };
    return { "@nestjs/swagger": { "models": [[import("./seperate.dto"), { "HelloClassSeperate": { name: { required: true, type: () => String } } }], [import("./app.dto"), { "GetHelloDto": { hello: { required: true, type: () => [HelloClass] } }, "GetHelloSeperateDto": { hello: { required: true, type: () => [t["./seperate.dto"].HelloClassSeperate] } } }]], "controllers": [[import("./app.controller"), { "AppController": { "getHello": { type: t["./app.dto"].GetHelloDto }, "getHelloSeperate": { type: t["./app.dto"].GetHelloSeperateDto } } }]] } };
};

Minimum reproduction code

https://github.com/evanwhitten/reproduce-cli-swagger-reference

Steps to reproduce

  1. nest start -b swc --type-check -> view error from invalid metadata.ts generated

Expected behavior

Expected to have Swagger definitions generated the same as when running the app without SWC.

Without SWC
Screenshot 2023-09-26 at 4 36 46 PM

With SWC
Screenshot 2023-09-26 at 4 37 39 PM

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

@nestjs/swagger

NestJS version

10.2.6

Packages versions

  "dependencies": {
    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/swagger": "^7.1.12",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@swc/cli": "^0.1.62",
    "@swc/core": "^1.3.89",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/node": "^20.3.1",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.59.11",
    "@typescript-eslint/parser": "^5.59.11",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-prettier": "^4.2.1",
    "jest": "^29.5.0",
    "prettier": "^2.8.8",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },

Node.js version

v18.16.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@evanwhitten evanwhitten added the needs triage This issue has not been looked into label Sep 26, 2023
@evanwhitten
Copy link
Author

Moved to correct repo nestjs/swagger#2636

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

1 participant