NodeJS 在nest js的swagger规范中有可能有泛型类型吗?

1dkrff03  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(149)

我正在实现一个POST端点,它将这样一个DTO作为输入:

export class GenericEventDto<D, A> {
  @ApiProperty({ type: D }) // <- does not work
  @Expose()
  data: D

  @ApiProperty({ type: A })
  @Expose()
  attributes: A // <- same

  @ApiProperty()
  @Expose()
  messageId: string
}

在swagger文档中,dataattributes props似乎是空对象。控制器代码如下所示:

export class Controller {
  @ApiBody({ type: GenericEventDto<ConcreteData, ConcreteAttributes> })
  async handleIncomingMessage(
    @Body() event: GenericEventDto<ConcreteData, ConcreteAttributes>
  ): Promise<void> {
    // do smth
  }
}

在swagger规范中有没有可能有泛型类型?此GenericEventDto稍后将被其他端点重用,我希望避免代码重复

4zcjmb1e

4zcjmb1e1#

文件上特别提到了
由于TypeScript不存储有关泛型或接口的元数据,因此当您在DTO中使用它们时,SwaggerModule可能无法在运行时正确生成模型定义
您需要创建一个mixin而不是泛型类型。在这种情况下,mixin是返回类定义的函数

相关问题