Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the response to be an array in the swagger response using DTOs

some-dto.ts

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

      @ApiProperty()
      age: number;

      @ApiProperty()
      breed: string;
    }

I don't want response something like this:

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

but my response must be array of like dto objects. I want smth like soo

    ApiOkResponse(
          description: 'The record has been successfully removed.',
          schema: {
            type: 'array',
            properties: {
              obj: {
                type: CreateCatDto
              }
            }
          }
        )
like image 582
har17bar Avatar asked Mar 09 '20 11:03

har17bar


3 Answers

have you tried something like this:

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

Let me know if it helps

like image 131
A. Maitre Avatar answered Dec 31 '22 08:12

A. Maitre


I found another solution we can wrap in array like this

@ApiOkResponse(
  description: 'Cat object',
  type: [CreateCatDto]
)
like image 44
har17bar Avatar answered Dec 31 '22 08:12

har17bar


And if you need to deal with multiple types :

@ApiOkResponse({
  description: 'More or less dangerous animals',
  schema: {
    type: 'array',
    items: {
      oneOf: [
        { $ref: getSchemaPath(CreateCatDto) },
        { $ref: getSchemaPath(CreateAlligatorDto) }
      ],
    },
  },
})
like image 33
Remi Mélisson Avatar answered Dec 31 '22 10:12

Remi Mélisson