14

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
              }
            }
          }
        )
har17bar
  • 728
  • 1
  • 8
  • 22

3 Answers3

25

have you tried something like this:

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

Let me know if it helps

A. Maitre
  • 2,985
  • 21
  • 25
  • Do you have an idea if you want to respond with a list of lists? Like CreateCatDto[][]? – user1961312 Nov 01 '22 at 11:16
  • I personally haven't had to deal with such structure in past experiences. You would have to check on the web to see if this is supported and how to implement. If supported my guess is that you would need to tweak things in order to make it work Let us know if you find a solution though ! – A. Maitre Nov 20 '22 at 15:15
12

I found another solution we can wrap in array like this

@ApiOkResponse(
  description: 'Cat object',
  type: [CreateCatDto]
)
har17bar
  • 728
  • 1
  • 8
  • 22
6

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) }
      ],
    },
  },
})

Additionally, you might need to include them in then controller beforehand :

@Controller("noahsark")
@ApiExtraModels(CreateCatDto, CreateAlligatorDto)
export class NoahsArkController {
...
}

Remi Mélisson
  • 2,724
  • 2
  • 21
  • 13