Swagger: Array of multiple types in response not working

Created on 13 May 2019  路  6Comments  路  Source: nestjs/swagger

Version : "@nestjs/swagger": "^2.5.1"

I'm trying to print out an API response with a list of various object types. I'm also trying to use class-transformer with discriminators but swagger prints out [{}]

export class GetPageDto extends WeakCreatePageDto {
  @ApiModelPropertyOptional({
    isArray: true,
  })
  @IsOptional()
  @IsArray()
  @Type(() => BaseBlockDto, {
    discriminator: {
        property: 'type',
        subTypes: [
            { value: GetBannerDto, name: 'banner' },
            { value: GetCampaignDto, name: 'campaign' },
        ],
    },
  })
  readonly blockList: GetBannerDto | GetCampaignDto;
}

Is there a way to define an array of multiple types ?

question

Most helpful comment

Same observation today.
@ApiModelProperty({ isArray: true, type: [FooDto] }) readonly items: FooDto[]
leads to type being empty in .json.

EDIT:
Got this working...
Apparently out of 5 known (to me) possibilities:

  1. @ApiModelProperty({ type: [FooDto] }) readonly items: FooDto[]
  2. @ApiModelProperty({ isArray: true, type: FooDto }) readonly items: FooDto[]
  3. @ApiModelProperty({ isArray: true }) readonly items: FooDto[]
  4. @ApiModelProperty({ isArray: true, type: [FooDto] }) readonly items: FooDto[]
  5. @ApiModelProperty() readonly items: FooDto[]
    only first 2 work.

It is closest to what is described in the 'quickstart' as well. So I would not dare to say 'docs' are wrong... but still... it could be a little confusing. ;)

All 6 comments

Same observation today.
@ApiModelProperty({ isArray: true, type: [FooDto] }) readonly items: FooDto[]
leads to type being empty in .json.

EDIT:
Got this working...
Apparently out of 5 known (to me) possibilities:

  1. @ApiModelProperty({ type: [FooDto] }) readonly items: FooDto[]
  2. @ApiModelProperty({ isArray: true, type: FooDto }) readonly items: FooDto[]
  3. @ApiModelProperty({ isArray: true }) readonly items: FooDto[]
  4. @ApiModelProperty({ isArray: true, type: [FooDto] }) readonly items: FooDto[]
  5. @ApiModelProperty() readonly items: FooDto[]
    only first 2 work.

It is closest to what is described in the 'quickstart' as well. So I would not dare to say 'docs' are wrong... but still... it could be a little confusing. ;)

@arekbal only first 2 are valid

@elSuperRiton we can't resolve union types, this is related to this issue https://github.com/nestjs/swagger/issues/191

@kamilmysliwiec what specifically from https://github.com/nestjs/swagger/issues/191 does not resolving union types have to do with so that I can follow along?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fiorello picture Fiorello  路  5Comments

Diluka picture Diluka  路  4Comments

yuval-hazaz picture yuval-hazaz  路  3Comments

kalaivanan-muthusamy picture kalaivanan-muthusamy  路  4Comments

dennisameling picture dennisameling  路  4Comments