NestJs Swagger mixed types

本秂侑毒 提交于 2020-12-15 06:20:09

问题


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>;

update

As @Youba suggeted answer 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


回答1:


Please try

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

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



来源:https://stackoverflow.com/questions/64939247/nestjs-swagger-mixed-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!