【问题标题】:NestJs Swagger mixed typesNestJs Swagger 混合类型
【发布时间】:2021-03-04 10:42:48
【问题描述】:

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

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

更新

正如@Youba 建议的答案,我试过了

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

但结果如下图,不正确

【问题讨论】:

    标签: typescript swagger nestjs openapi


    【解决方案1】:

    请尝试

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

    Array&lt;TItem&gt; 可以在 OpenAPI 中用{type: 'array', items: { type: TItem } } 表示

    【讨论】:

      猜你喜欢
      • 2015-11-04
      • 2020-04-25
      • 2021-09-20
      • 2021-06-07
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      • 2021-12-15
      • 2021-10-18
      相关资源
      最近更新 更多