使用AWS API Gateway的OpenAPI / Swagger模型继承

我正在尝试使用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不支持discriminatorref)。

好,很烦人,但一种解决方法是使用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也不支持oneOfref)。

是否有人知道如何使用AWS API Gateway来实现这种性质的模型架构,特别是要利用主体验证的继承模式(Pet <- Dog)?或者确实是一种解决方法,而不必为每种具体类型都拥有方法?

zx705 回答:使用AWS API Gateway的OpenAPI / Swagger模型继承

对于您的问题,这可能不是完全令人满意的答案,但有一种方法可以将 oneOf 与 API Gateway 结合使用。 You can use JSON schema for individual models according to AWS

因此,您可以在部署 API Gateway 后更新模型。

# Reformatted here for readability
VALUE='"{\"$schema\": \"http://json-schema.org/draft-04/schema#\",\"title\": \"A Pet Request\",\"oneOf\":
        [{ \"$ref\": \"https://apigateway.amazonaws.com/restapis/xxxxxxx/models/Cat\" },{ \"$ref\": \"https://apigateway.amazonaws.com/restapis/xxxxxxx/models/Dog\" }]}"'


aws apigateway update-model \
--rest-api-id xxxxxxx \
--model-name 'PetRequest' \
--patch-operations "op=replace,path=/schema,value=${VALUE}"

此解决方案有效,但可能不太可持续,因为您需要在每次部署后执行 patch-operations

如果我找到更好的方法,我可能会更新这个答案。

,

我想添加另一个答案,以更漂亮的方式解决问题!关键问题是,在之前的尝试中,我使用了 OpenAPI 3.0.0,但是 3.0.1 似乎不再对 oneOf 指令有任何问题。

这是工作示例:

{
  "openapi": "3.0.1","info": {
    // (...)
  },"paths": {
    "/pets": {
      "post": {
        "summary": "Post a pet","requestBody": {
          "required": true,"content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PetRequest"
              }
            }
          }
        },"responses": {
          "201": {
            // (...)
          }
        }
      }
    }
  },"components": {
    "schemas": {
      "Cat": {
        "type": "object","required": [
          "cat_stuff"
        ],"properties": {
          "cat_stuff": {
            "type": "string"
          }
        }
      },"Dog": {
        "type": "object","required": [
          "dog_stuff"
        ],"properties": {
          "dog_stuff": {
            "type": "string"
          },}
      },"PetRequest": {
        "oneOf": [
          {
            "$ref": "#/components/schemas/Cat"
          },{
            "$ref": "#/components/schemas/Dog"
          }
        ]
      }
    },// Many fields omitted (...)
}

使用与这些架构之一不匹配的有效负载进行卷曲会产生以下错误,这证明 oneOf 按预期工作!

(...)
"error_messages": [
    "[instance failed to match exactly one schema (matched 0 out of 2)]"
],

请自行测试并留下一些反馈。

本文链接:https://www.f2er.com/3078537.html

大家都在问