如何解决如何在 OpenApi 中将 OneOf 与两个相似的模式一起使用?
我在 openapi 中有以下架构
/submit:
post:
description: Submit info
x-openapi-router-controller: abc.def
operationId: submit_info
requestBody:
description: Submit request
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/SubmitRequest'
responses:
200:
description: submitted successfully
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/FooResponse'
- $ref: '#/components/schemas/BarResponse'
FooResponse:
type: object
required:
- id
- value
properties:
id:
type: string
description: id
example: '1234'
value:
type: string
description: value
example: 'foo'
BarResponse:
type: object
required:
- id
- value
- data
properties:
id:
type: string
description: id
example: '1234'
value:
type: string
description: value
example: 'foo'
data:
type: object
required:
- transaction_id
description: Data associated bar response
properties:
transaction_id:
type: string
description: transaction id
example: 'c2345'
所以我有 FooResponse 和 BarResponse。尽管我在 FooResponse 和 BarResponse 中有不同的必需项,但我得到 OneOf 匹配多个模式错误。即使其中一个架构中有额外的必需项,openapi 似乎也匹配了两个架构。
有没有办法解决这个问题?我想使用 OneOf,但不确定如何区分这两种架构。
感谢您的帮助。
解决方法
OpenAPI Schema Object 有一个名为 additionalProperties
的属性,用于指定在实例中是否允许架构中未定义的属性(这些属性将被忽略)。
additionalProperties
的默认值为 true
,这将使 FooResponse
和 BarResponse
对您的实例有效。
在 additionalProperties
上将 false
设置为 FooResponse
应该可以解决问题。
如果您不想禁止其他属性,则必须在两个对象中引入某种 type
属性并使用 Discriminator Object。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。