NestJS - Swagger - 显示所有枚举值

NestJS - Swagger - Show all enum values

我想在 swagger UI.

的正文部分看到关于我的 dto 属性 的所有枚举值

我把这个@ApiQuery 装饰器放在我的代码中:

@ApiQuery({
  name: 'name',
  enum: ENUM_NAME,
  isArray: true,
})
@Post()
async name(...):Promise<...> {...}

但这与@Query 装饰器一起使用,让 swagger 的过滤器起作用。 (在 NestJS Swagger 文档中查找)。

所以我没有像你看到的那样在我的代码中放入@Query,因为我想要我的 dto 中的枚举值。

This is the result

但它不喜欢这个解决方案。这是一个解决方法。

有没有更好的方法来实现这个结果?

在swagger的正文部分是不可能像查询部分那样做的,但是你可以这样做:

// app.controller.ts

@Post()
public async name(@Body() dto: NameDto): Promise<void> {}

// name.dto.ts

// This is enum for demo
enum Demo {
    DEMO_1 = 'DEMO_1',
    DEMO_2 = 'DEMO_2',
}

export class NameDto {
    @ApiProperty({
        enum: Demo,
        isArray: true,
        example: [Demo.DEMO_1, Demo.DEMO_1],
    })
    public name: Demo[];
}

结果=>

或者如果你不想使用 dto

// app.controller.ts

@Post()
@ApiBody({
    schema: {
       type: 'object',
         properties: {
            name: {
              type: 'array',
              items: {
                 enum: [Demo.DEMO_1, Demo.DEMO_2],
                 example: [Demo.DEMO_1, Demo.DEMO_2],
              },
            },
        },
    },
})
public async name(@Body() dto: any): Promise<void> {}