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

Breaks swagger plugin #188

Closed
lepicekmichal opened this issue Feb 4, 2020 · 9 comments
Closed

Breaks swagger plugin #188

lepicekmichal opened this issue Feb 4, 2020 · 9 comments
Labels
bug Something isn't working needs repro script This dosnt have an reproduce script stale This Issue | PR had no activity in a while and will be closed if it stays so uses: nestjs & nestjs-typegoose This Issue is using nestjs-typegoose

Comments

@lepicekmichal
Copy link

lepicekmichal commented Feb 4, 2020

Versions

  • NodeJS: 10.18.1
  • Typegoose(NPM): 6.2.2
  • mongoose: 5.7.0
  • mongodb: 4.2.3
  • nestjs / nestjs-typegoose: 7.0.0

What is the Problem?

When I use typegoose, it breaks swagger plugin for automatic generation of documentation.
https://trilon.io/blog/nestjs-swagger-4-whats-new#Introducing-the-NEW-Nest-Swagger-Plugin-
https://github.com/nestjs/swagger/tree/master/lib/plugin

nest-cli.json

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "plugins": [
      {
        "name": "@nestjs/swagger/plugin",
      }
    ]
  }
}

any.dto.json

@index({whatever: 1})
export class Whatever {
  @prop({
    required: true,
    lowercase: true,
  })
  public readonly whatever!: string
}

Do you know why it happenes?

no

(node:30) UnhandledPromiseRejectionWarning: Error: A circular dependency has been detected (property key: "name"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
api_1  |     at SchemaObjectFactory.createNotBuiltInTypeReference (/home/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:107:19)
api_1  |     at SchemaObjectFactory.mergePropertyWithMetadata (/home/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:88:25)
api_1  |     at modelProperties.map.key (/home/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:55:35)
api_1  |     at Array.map (<anonymous>)
api_1  |     at SchemaObjectFactory.exploreModelSchema (/home/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:54:52)
api_1  |     at ResponseObjectFactory.create (/home/app/node_modules/@nestjs/swagger/dist/services/response-object-factory.js:45:47)
api_1  |     at lodash_1.mapValues (/home/app/node_modules/@nestjs/swagger/dist/explorers/api-response.explorer.js:50:96)
api_1  |     at /home/app/node_modules/lodash/lodash.js:13401:38
api_1  |     at /home/app/node_modules/lodash/lodash.js:4905:15
api_1  |     at baseForOwn (/home/app/node_modules/lodash/lodash.js:2990:24)
api_1  |     at Function.mapValues (/home/app/node_modules/lodash/lodash.js:13400:7)
api_1  |     at mapResponsesToSwaggerResponses (/home/app/node_modules/@nestjs/swagger/dist/explorers/api-response.explorer.js:50:39)
api_1  |     at exports.exploreApiResponseMetadata (/home/app/node_modules/@nestjs/swagger/dist/explorers/api-response.explorer.js:26:16)
api_1  |     at explorers.reduce (/home/app/node_modules/@nestjs/swagger/dist/swagger-explorer.js:67:45)
api_1  |     at Array.reduce (<anonymous>)
api_1  |     at lodash_1.mapValues (/home/app/node_modules/@nestjs/swagger/dist/swagger-explorer.js:66:99)
@lepicekmichal lepicekmichal added the bug Something isn't working label Feb 4, 2020
@hasezoey hasezoey added the uses: nestjs & nestjs-typegoose This Issue is using nestjs-typegoose label Feb 4, 2020
@hasezoey
Copy link
Member

hasezoey commented Feb 4, 2020

@lepicekmichal please provide your code to reproduce the issue

@hasezoey hasezoey added the needs repro script This dosnt have an reproduce script label Feb 4, 2020
@lepicekmichal
Copy link
Author

@hasezoey no need, just turn it on.
but sure, code provided

@hasezoey
Copy link
Member

hasezoey commented Feb 4, 2020

@lepicekmichal i dont know what you mean with "just turn it on" i never used nest outside of typegoose debugging and never swagger - so please provide steps to for me to reproduce the issue

@lepicekmichal
Copy link
Author

Alright, but then it's too much code to share. Just create simplest app in nestjs (nest can generate it) and use typegoose in it.

@hasezoey
Copy link
Member

hasezoey commented Feb 4, 2020

@lepicekmichal just create an reproduction repository - with instructions how to start and where to look for the error

@hasezoey hasezoey added the stale This Issue | PR had no activity in a while and will be closed if it stays so label Jul 15, 2020
@hasezoey
Copy link
Member

closing because no further information was given and is stale since February

@Bratishques
Copy link

Hi, I know the issue is closed, but if you stumble upon this post while googling the issue, I have managed to reproduce the issue with following packages

"reflect-metadata": "0.1.13", "@typegoose/typegoose": "12.2.0", "@nestjs/swagger": "7.3.0",

The issue goes as following: both NestJs swagger and typegoose use Reflect.getMetadata in order to get the data about your classes, but when you import some typegoose functions that use Reflect the object gets redefined in global scope and method works differently. Kinda sloppy explanation, but this is what I found out during my nightly monkey patching session.

Fix: bump your reflect-metadata package, it somehow fixes it

@hasezoey
Copy link
Member

hasezoey commented Mar 9, 2024

@Bratishques the problem is that reflect-metadata 0.1.x always modified the global Reflect object (to my understanding), in 0.2.x, there is a option to use a non-global version, but typegoose is not making use of that at the moment.
FYI typegoose uses reflect-metdata 0.2.1 since 12.1.0.

also please note that currently reflect-metadata is not a peer-dependency and cannot be replaced at the moment, see #761.

@Bratishques
Copy link

@Bratishques the problem is that reflect-metadata 0.1.x always modified the global Reflect object (to my understanding), in 0.2.x, there is a option to use a non-global version, but typegoose is not making use of that at the moment. FYI typegoose uses reflect-metdata 0.2.1 since 12.1.0.

also please note that currently reflect-metadata is not a peer-dependency and cannot be replaced at the moment, see #761.

Thanks for the explanation, I personally hate when something decides to go global, stuff just breaks. I guess you understood it, but I will clarify: when I said bump your reflect-metadata package, it somehow fixes it I meant that the person that is googling the issue will resolve by doing so, typegoose itself causes no issue. And thanks for your hard work maintaining the package!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs repro script This dosnt have an reproduce script stale This Issue | PR had no activity in a while and will be closed if it stays so uses: nestjs & nestjs-typegoose This Issue is using nestjs-typegoose
Projects
None yet
Development

No branches or pull requests

3 participants