我正在实现一个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文档中,data
和attributes
props似乎是空对象。控制器代码如下所示:
export class Controller {
@ApiBody({ type: GenericEventDto<ConcreteData, ConcreteAttributes> })
async handleIncomingMessage(
@Body() event: GenericEventDto<ConcreteData, ConcreteAttributes>
): Promise<void> {
// do smth
}
}
在swagger规范中有没有可能有泛型类型?此GenericEventDto
稍后将被其他端点重用,我希望避免代码重复
1条答案
按热度按时间4zcjmb1e1#
文件上特别提到了
由于TypeScript不存储有关泛型或接口的元数据,因此当您在DTO中使用它们时,SwaggerModule可能无法在运行时正确生成模型定义
您需要创建一个mixin而不是泛型类型。在这种情况下,mixin是返回类定义的函数