typescript - NestJs Swagger 混合类型

标签 typescript swagger nestjs openapi

我有一个类,其中一个属性可以是字符串或字符串数​​组,不知道我应该如何在 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,
    })
但结果如下图,这是不正确的
enter image description here

最佳答案

请尝试

@ApiProperty({
   oneOf: [
      { type: 'string' },
      { 
         type: 'array',
         items: {
            type: 'string'
         }
      }
   ]
})
Array<TItem>可以在 OpenAPI 中用 {type: 'array', items: { type: TItem } } 表示

关于typescript - NestJs Swagger 混合类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64939247/

相关文章:

nestjs - 如何在一个 DTO 中编写@Param 和@Body

javascript - Angular 4 : using component variables inside custom validator functions

c# - 什么是 'api_key' 以及如何正确使用它

java - JDK14 无法运行 "java --add-opens"

javascript - Nestjs 无法解决依赖关系

typescript - TypeORM OneToMany 导致 "ReferenceError: Cannot access ' <Entity >' before initialization"

typescript - StencilJS Prop 未填充在 JSX 中

node.js - express-generator-typescript 生成的包无法成功运行调试器

typescript - 如何在单独的定义文件中扩展 TypeScript 类定义?

python - 如何让 Swagger 生成的 Python 客户端工作?