nestjs / swagger

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

Circular dependency with generated swagger metadata #2849

Open Stono opened 7 months ago

Stono commented 7 months ago

Is there an existing issue for this?

Current behavior

Hi, I'm trying to use the generate metadata function of the cli and getting an error when using an object type. There seems to be several historical issues showing the same thing:

https://github.com/nestjs/swagger/issues/1442 https://github.com/nestjs/swagger/issues/502

The metadata generates fine, however when starting the app I get:

A circular dependency has been detected (property key: \"auditLogs\"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers (\"type: () => ClassType\").","stack":"Error: A circular dependency has been detected (property key: \"auditLogs\"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers (\"type: () => ClassType\").

This is my super simple dto:

export abstract class GetUserAuditLogResponse {
  public abstract user: string
  public abstract clusters: string[]
  public abstract auditLogs: { username: string }[]
}

Minimum reproduction code

Example in comments above

Steps to reproduce

No response

Expected behavior

It to handle the array of objects

Package version

7.3.0

NestJS version

10.3.3

Node.js version

20

In which operating systems have you tested?

Other

No response

Stono commented 7 months ago

This could be user error, as i'm new to this... but moving { username: string } into it's own class def "fixes" it, eg:

export abstract class AuditLogItem {
  public abstract username: string 
}

export abstract class GetUserAuditLogResponse {
  public abstract user: string
  public abstract clusters: string[]
  public abstract auditLogs: AuditLogItem[]
}
cjam commented 6 months ago

Was experiencing the same thing on another project. After a lot of trial and error I found that my typeorm package had been updated to 0.3.20, on a hunch I rolled it back to the previous version we were using (0.3.17) which worked. I then worked my way back up and was able to install 0.3.19 without this issue, but as soon as I installed 0.3.20 it failed.

Did a little more searching and eventually found that it seems this has been logged against typeorm here https://github.com/typeorm/typeorm/issues/10671

Not sure if it's the same issue or different, but adding in my meanderings anyways.

lacuong2009 commented 6 months ago

Was experiencing the same thing on another project. After a lot of trial and error I found that my typeorm package had been updated to 0.3.20, on a hunch I rolled it back to the previous version we were using (0.3.17) which worked. I then worked my way back up and was able to install 0.3.19 without this issue, but as soon as I installed 0.3.20 it failed.

Did a little more searching and eventually found that it seems this has been logged against typeorm here typeorm/typeorm#10671

Not sure if it's the same issue or different, but adding in my meanderings anyways.

The same to me, Rollbacked typeORM to 0.3.19. it works fine

OSA413 commented 6 months ago

Possible duplicate of https://github.com/nestjs/swagger/issues/2811

Gavin152 commented 5 months ago

I can confirm that hard-coding typeORM to 0.3.19 fixes the issue.