Nestjs Swagger 4 的循环依赖
Posted
技术标签:
【中文标题】Nestjs Swagger 4 的循环依赖【英文标题】:Circular Dependency with Nestjs Swagger 4 【发布时间】:2020-06-30 03:09:07 【问题描述】:当我将 @nest/swagger 库更新到版本 4 时,发生了这个错误:
(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)
我的模型类似乎是这样的:
@Entity()
export class Job
.
.
.
@ManyToOne(type => Customer, customer => customer.jobs)
@ApiProperty( type: Customer )
customer: Customer;
【问题讨论】:
【参考方案1】:对我有用的解决方案是在 @ApiProperty() 中使用 箭头函数 声明 type,如下所示:
@Entity()
export class Job
.
.
.
@ManyToOne(type => Customer, customer => customer.jobs)
@ApiProperty( type: () => Customer )
customer: Customer;
【讨论】:
我还是有这个问题。 UnhandledPromiseRejectionWarning:错误:已检测到循环依赖项(属性键:“KM”)。请确保双向关系的每一方都使用惰性解析器(“type: () => ClassType”)。 这是我的枚举 enum EmissionUnitEnum KM = 'km', MINUTE = 'minute', MINUTES = 'minutes', HOUR = 'hour', HOURS = 'hours', FIXED = 'fixed' , SERVING = '服务', 如果你的Customer
类也包含 @ApiProperty
装饰器,别忘了在那里也使用箭头函数。【参考方案2】:
至少还有另外三种情况会出现相同的错误消息,即使它们与双向关系无关:
枚举类型
错误:
@ApiProperty(
type: Salutation
)
public salutation: Salutation;
正确:
@ApiProperty(
enum: Salutation
)
public salutation: Salutation;
匿名类型
错误:
@ApiProperty(
)
public address:
street: string;
houseNumber: string;
;
正确:
@ApiProperty(
type: Address
)
public address: Address;
空
错误:
@ApiProperty(
description: 'This always returns null for downward compatibility'
)
public someLegacyField: null;
正确:
@ApiProperty(
description: 'This always returns null for downward compatibility',
type: String; // needed to avoid error
)
public someLegacyField: null;
为此我在 Github 上创建了一个问题:https://github.com/nestjs/swagger/issues/1475
【讨论】:
【参考方案3】:对于同样遇到此问题的任何人,您可以将type
键更改为@ApiProperty
上的enum
键。这对我有用。
【讨论】:
你是最棒的!【参考方案4】:我最近也遇到了这个问题。
就我而言,我在自定义验证文件夹的同一个文件中使用了一些验证器。仅在nest build
和node dist/main
之后出现错误“检测到循环依赖”。
我不确定这是否是 NestJs + Swagger 在生成构建后的问题。显然,我的解决方法是将验证器放入多个文件(每个文件包含 1 个验证器),它对我有用。
【讨论】:
以上是关于Nestjs Swagger 4 的循环依赖的主要内容,如果未能解决你的问题,请参考以下文章