我正在尝试使用AWS API Gateway实现在Swagger或OpenAPI 3.0中定义的API。
此API中的一个端点采用抽象的基本模型(将其命名为Pet
与陈旧的Swagger示例一致),但实际上期望从Pet
派生的具体模型。 .. Dog
。
具体模型可以通过type
上的Pet
属性来确定。
具体模型可以添加其他字段。
当然,这是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
(摘自here)
但是,AWS API Gateway不支持discriminator
(ref)。
好,很烦人,但一种解决方法是使用OpenAPI 3.0定义API,并在模式中使用oneOf
:
paths:
/pets:
patch:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
但是(再次),AWS API Gateway也不支持oneOf
(ref)。
是否有人知道如何使用AWS API Gateway来实现这种性质的模型架构,特别是要利用主体验证的继承模式(Pet <- Dog
)?或者确实是一种解决方法,而不必为每种具体类型都拥有方法?