Nestjs Swagger 4的循环依赖

时间:2020-03-19 00:45:25

标签: swagger nestjs circular-dependency

当我将 @ 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;
}

3 个答案:

答案 0 :(得分:9)

最适合我的解决方案是使用箭头功能 @ApiProperty()中声明类型,如下所示:

@Entity()
export class Job {
.
.
.
    @ManyToOne(type => Customer, customer => customer.jobs)
    @ApiProperty({ type: () => Customer })
    customer: Customer;
}

答案 1 :(得分:0)

对于同样遇到此问题的任何人,您可以将 type 上的 enum 键更改为 @ApiProperty 键。这对我有用。

答案 2 :(得分:0)

至少还有三种情况您会收到相同的错误消息,即使它们与双向关系无关:

枚举类型

错误:

print > "File" $1 ".csv"

正确:

>

匿名类型

错误:

@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;

我为此在 Github 上创建了一个问题:https://github.com/nestjs/swagger/issues/1475