【问题标题】:how to set the response to be an array in the swagger response using DTOs如何使用 DTO 将响应设置为招摇响应中的数组
【发布时间】:2020-06-21 06:23:18
【问题描述】:

some-dto.ts

    export class CreateCatDto {
      @ApiProperty()
      name: string;

      @ApiProperty()
      age: number;

      @ApiProperty()
      breed: string;
    }

我不想要这样的回应:

  @ApiOkResponse(
      description: 'Cat object',
      type: CreateCatDto
    )

但我的响应必须是类似 dto 对象的数组。 我想要像soo这样的东西

    ApiOkResponse(
          description: 'The record has been successfully removed.',
          schema: {
            type: 'array',
            properties: {
              obj: {
                type: CreateCatDto
              }
            }
          }
        )

【问题讨论】:

    标签: nestjs nestjs-swagger


    【解决方案1】:

    你有没有尝试过这样的事情:

    @ApiOkResponse(
        description: 'Cat object',
        type: CreateCatDto,
        isArray: true // <= diff is here
    )
    

    如果有帮助请告诉我

    【讨论】:

      【解决方案2】:

      我找到了另一种解决方案,我们可以像这样将其包装在数组中

      @ApiOkResponse(
        description: 'Cat object',
        type: [CreateCatDto]
      )
      

      【讨论】:

      • 很高兴知道数组类型可以通过在数组中使用 DTO 类型来推断!
      猜你喜欢
      • 2017-01-27
      • 2019-04-05
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多