循环依赖与Nestjs Swagger 4

lyfkaqu1  于 2022-11-06  发布在  其他
关注(0)|答案(6)|浏览(294)

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

zynd9foi1#

对我有效的解决方案是在**@ApiProperty()中使用箭头函数声明类型**,如下所示:

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

sc4hvdpw2#

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

枚举为类型

错:

@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

stszievb

stszievb3#

对于同样有这个问题的人,你可以把type键改成@ApiProperty上的enum键。这对我很有效。

sdnqo3pr

sdnqo3pr4#

我最近也遇到了这个问题。在我的例子中,我在我的custom-validations文件夹的同一个文件中使用了几个验证器。错误“A circular dependency has been detected”只出现在nest buildnode dist/main之后。
我不确定这是否是NestJs + Swagger在生成构建后的问题。显然,我的修复是将验证器放入几个文件中(每个文件包含一个验证器),它对我很有效。

jdzmm42g

jdzmm42g5#

我在实体的嵌套属性上使用type interfaces时遇到了这个问题

不正确:

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

Nest.js建议使用类来代替--即使是在嵌套属性上:

正确答案:

export class BookLikes {
  bookLikes: BookLike[];
}

export class BookLike {
  user: User;
  book: Book;
}
20jt8wwn

20jt8wwn6#

看看这个bugfix
我设法使我的枚举(ProcessCat)像这样工作:

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

因此NestJS可以正确编译。

相关问题