我正在尝试使用AWS API Gateway实现Swagger或OpenAPI 3.0中定义的API。
在此API中,一个端点接受一个抽象基础模型(我们称之为
可以通过
具体模型可以添加其他字段。
当然,这是一个
在此API中,一个端点接受一个抽象基础模型(我们称之为
Pet
以保持与广为流传的Swagger示例的一致性),但实际上需要从Pet
派生的具体模型,例如Dog
。可以通过
Pet
上的type
属性确定具体模型。具体模型可以添加其他字段。
当然,这是一个
discriminator
的工作:definitions:
Pet:
discriminator: petType
required:
- name
- petType # required for inheritance to work
properties:
name:
type: string
petType:
type: string
Cat:
allOf:
- $ref: '#/definitions/Pet' # Cat has all properties of a Pet
- properties: # extra properties only for cats
huntingSkill:
type: string
default: lazy
enum:
- lazy
- aggressive
Dog:
allOf:
- $ref: '#/definitions/Pet' # Dog has all properties of a Pet
- properties: # extra properties only for dogs
packSize:
description: The size of the pack the dog is from
type: integer
(来源:这里)
然而,AWS API Gateway 不支持discriminator
(参考)。
好吧,这很令人烦恼,但也许我们可以通过使用 OpenAPI 3.0 在模式中利用oneOf
来定义API:
paths:
/pets:
patch:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
然而(再次强调),AWS API Gateway也不支持oneOf
(参考文献)。
有没有人知道如何在 AWS API Gateway 中实现这种模型架构,特别是利用继承模式(Pet <- Dog
)的 body 验证优势?或者有没有一种方法可以避免为每个具体类型都编写方法的解决方法?