29

When I updated the @nest/swagger library to version 4, this error happened:

(node:16134) UnhandledPromiseRejectionWarning: Error: A circular dependency has been detected (property key: "customer"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
    at SchemaObjectFactory.createNotBuiltInTypeReference (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:182:19)
    at SchemaObjectFactory.mergePropertyWithMetadata (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:117:25)
    at /opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:66:35
    at Array.map (<anonymous>)
    at SchemaObjectFactory.exploreModelSchema (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:65:52)
    at SchemaObjectFactory.createNotBuiltInTypeReference (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:187:37)
    at SchemaObjectFactory.mergePropertyWithMetadata (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:117:25)
    at /opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:66:35
    at Array.map (<anonymous>)
    at SchemaObjectFactory.exploreModelSchema (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:65:52)

My model class seems to this:

@Entity()
export class Job {
.
.
.
    @ManyToOne(type => Customer, customer => customer.jobs)
    @ApiProperty({ type: Customer })
    customer: Customer;
}
btd1337
  • 2,616
  • 2
  • 16
  • 25

8 Answers8

53

The solution that worked for me was to declare in @ApiProperty() the type with arrow function, like below:

@Entity()
export class Job {
.
.
.
    @ManyToOne(type => Customer, customer => customer.jobs)
    @ApiProperty({ type: () => Customer })
    customer: Customer;
}
btd1337
  • 2,616
  • 2
  • 16
  • 25
  • 1
    I still have the problem. UnhandledPromiseRejectionWarning: Error: A circular dependency has been detected (property key: "KM"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType"). – Balaji Venkatraman Jun 01 '21 at 20:08
  • Here is my enum enum EmissionUnitEnum { KM = 'km', MINUTE = 'minute', MINUTES = 'minutes', HOUR = 'hour', HOURS = 'hours', FIXED = 'fixed', SERVING = 'serving', } – Balaji Venkatraman Jun 01 '21 at 20:08
  • If your `Customer` class also contains `@ApiProperty` decorators, don't forget to use arrow functions there as well. – Elias Strehle Oct 20 '21 at 09:39
35

There are at least three more cases where you get the same error message, even though they have nothing to do with bidirectional relationships:

Enum as type

Wrong:

@ApiProperty({
    type: Salutation
})
public salutation: Salutation;

Correct:

@ApiProperty({
    enum: Salutation
})
public salutation: Salutation;

Anonymous types

Wrong:

@ApiProperty({
})
public address: {
    street: string;
    houseNumber: string;
};

Correct:

@ApiProperty({
    type: Address
})
public address: Address;

null

Wrong:

@ApiProperty({
    description: 'This always returns null for downward compatibility'
})
public someLegacyField: null;

Correct:

@ApiProperty({
    description: 'This always returns null for downward compatibility',
    type: String; // needed to avoid error
})
public someLegacyField: null;

I created an issue on Github for this: https://github.com/nestjs/swagger/issues/1475

Digital Alpha
  • 226
  • 1
  • 12
user3817008
  • 613
  • 7
  • 13
8

For anyone who had this problem as well, you can change the type key to the enum key on the @ApiProperty. This worked for me.

danibrum
  • 459
  • 8
  • 21
2

I encountered this issue when I used type interfaces on nested properties of an entity

Incorrect:

export class BookLikes {
  bookLikes: {
    user: User;
    book: Book;
  }[];
}

Nest.js recommends using classes instead - even on nested properties:

Correct:

export class BookLikes {
  bookLikes: BookLike[];
}

export class BookLike {
  user: User;
  book: Book;
}
Leroy Dunn
  • 361
  • 4
  • 7
1

Take a look at this bugfix

I managed to make my enum (ProcessCat) working like this:

  @ApiProperty({
    enum: ProcessCat,
    enumName: 'ProcessCat',
    isArray: true,
  })
  category: ProcessCat;

And thus the NestJS compiles properly.

Bullsized
  • 362
  • 4
  • 7
1

I had this problem using Swagger Ui Cli with Webpack every time I defined an Array property. My solution was to hardcode the ApiPropterty as Array because it seems like the plugin was not picking up on it for some reason.

import { ApiProperty } from '@nestjs/swagger'

export class CreateCatDto {
  @ApiProperty({ type: [String] })
  kittenNames: string[]
}

Docs: https://docs.nestjs.com/openapi/types-and-parameters#arrays

Also if everything else fails rename your files to something the plugin does not pick up. Default: remove entity or dto from the name.

IkeRoyle
  • 457
  • 4
  • 13
0

I've also encountered this issue recently. In my case, I've used a few validators in the same file in my custom-validations folder. The error "A circular dependency has been detected" only appears after nest build and node dist/main.

I'm not sure if this is an issue of NestJs + Swagger after generating a build or not. Apparently, my fix was to put the validators into several files (each file contains 1 validator), it works for me.

enter image description here

Terry Truong
  • 291
  • 3
  • 4
0

I faced the same problem at it made me crazy. The answers here didn't really help. I caught it by chance, which is I defined a property as array, but without the IsArray decorator.

Bad:

grounding_docs: { name: string; url: string; }[];

Good:

@IsArray()
grounding_docs: { name: string; url: string; }[];
Mahmoud
  • 456
  • 3
  • 13