Swagger文档解释了如何定义包含混合类型的数组。,如["foo", 5, -2, "bar"]。但是,如何定义数组必须包含一种类型的项(字符串、["foo", "bar"])或另一种类型的项(整数、[5, -2])?
我尝试过这样做,但是Swagger UI无法呈现它,所以我想它是错误的:
oneOf:
- items:
- $ref: '#/components/schemas/SchemaA'
- items:
- $ref: '#/components/schemas/SchemaB'发布于 2019-06-20 08:30:13
首先,请记住,oneOf只支持OpenAPI 3.0 (openapi: 3.0.0),而不支持OpenAPI 2.0 (swagger: '2.0')。
您的场景可以使用oneOf来定义,如下所示:
oneOf:
- type: array
items:
type: string
- type: array
items:
type: integer
- type: array
items:
$ref: '#/components/schemas/SchemaA'在普通的JSON中,type: array可以从oneOf中移出并放在oneOf旁边,但我不确定OpenAPI是否允许这样做( OpenAPI规范在这一点上不清楚)。
type: array
oneOf:
- items:
type: string
- items:
type: integer
- items:
$ref: '#/components/schemas/SchemaA'我试过了,但是Swagger不能渲染它
目前,Swagger并不自动生成oneOf和anyOf模式的示例(参见本期)。解决方法是手动添加example和oneOf:
example: [1, 2, 3] # <------
oneOf:
- type: array
items:
type: string
- type: array
items:
type: integer
- type: array
items:
$ref: '#/components/schemas/SchemaA'https://stackoverflow.com/questions/56681569
复制相似问题