NestJs Swagger mixed types

Viewed 7473

I have a class that one of the properties can be string or array of strings, not sure how should I define it in swagger

    @ApiProperty({
        description: `to email address`,
        type: ???, <- what should be here?
        required: true,
    })
    to: string | Array<string>;

I tried

    @ApiProperty({
        description: `to email address(es)`,
        additionalProperties: {
            oneOf: [
                { type: 'string' },
                { type: 'Array<string>' },
            ],
        },
        required: true,
    })

and

    @ApiProperty({
        description: `to email address(es)`,
        additionalProperties: {
            oneOf: [
                { type: 'string' },
                { type: 'string[]' },
            ],
        },
        required: true,
    })

and

    @ApiProperty({
        description: `to email address(es)`,
        additionalProperties: {
            oneOf: [
                { type: 'string' },
                { type: '[string]' },
            ],
        },
        required: true,
    })

but the result is like below image, which is not correct

enter image description here

1 Answers

Please try

@ApiProperty({
   oneOf: [
      { type: 'string' },
      { 
         type: 'array',
         items: {
            type: 'string'
         }
      }
   ]
})

Array<TItem> can be expressed in OpenAPI with {type: 'array', items: { type: TItem } }

Related