给定以下 OpenAPI YAML,我们是否总是需要在创建 Pet
时定义 petType
?
我可以在不指定 petType
的情况下默认创建类型为 Dog
的宠物吗?有什么方法可以用 OpenAPI 做到这一点吗?
Pet:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Lizard'
discriminator:
propertyName: petType
最佳答案
do we always need to define
petType
while creating Pet
是的。如 Discriminator Object 中所述OpenAPI 规范部分(强调我的):
a discriminator MAY act as a "hint" to ... selection of the matching schema ... We can then describe exactly which field tells us which schema to use:
...
The expectation now is that a property with namepetType
MUST be present in ... payload, and the value will correspond to the name of a schema defined in the OAS document.
关于OpenAPI 鉴别器的一个默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63910059/