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;
}
Avoiding circular dependencies by refactoring The NestJS documentation advises that circular dependencies be avoided where possible. Circular dependencies create tight couplings between the classes or modules involved, which means both classes or modules have to be recompiled every time either of them is changed.
A forward reference allows Nest to reference classes which aren't yet defined using the forwardRef() utility function. For example, if CatsService and CommonService depend on each other, both sides of the relationship can use @Inject() and the forwardRef() utility to resolve the circular dependency.
A simple way to break the cycle is by telling Spring to initialize one of the beans lazily. So, instead of fully initializing the bean, it will create a proxy to inject it into the other bean. The injected bean will only be fully created when it's first needed.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With