NestJs Swagger 混合类型 [英] NestJs Swagger mixed types

查看:50
本文介绍了NestJs Swagger 混合类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,其中一个属性可以是字符串或字符串数​​组,不知道我应该如何在 swagger 中定义它

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

更新

作为@Youba 建议的答案,我尝试过

update

As @Youba suggeted answer I tried

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

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

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

但结果如下图,不正确

推荐答案

请尝试

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

Array 可以在 OpenAPI 中用 {type: 'array', items: { type: TItem } }

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

这篇关于NestJs Swagger 混合类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆