如何在整个 Swagger YAML 文档中重复使用我的 x-amazon-apigateway-integration 定义?

Mik*_*keO 6 yaml amazon-web-services swagger openapi

我目前正在使用 Swagger 来定义具有许多端点的 API,并且这些端点中的每一个都对“x-amazon-apigateway-integration”键具有相同的定义。我想在文档的某处定义它,并在整个过程中重复使用该定义。

要么我不明白应该如何定义定义,我没有将它放在正确的位置或两者的混合。我尝试在“定义”中定义这个定义,并作为它自己的密钥下的一些别名。定义(删除了关键信息)是:

x-amazon-apigateway-integration:
  responses:
    default:
      statusCode: '200'
  passthroughBehavior: when_no_match
  httpMethod: POST
  uri: >-
    arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda arn>/invocations
  credentials: '<role arn>'
  type: aws     
  requestTemplates: "application/json": "<object definition>"  
Run Code Online (Sandbox Code Playgroud)

我尝试将其定义为它自己的密钥下的别名(不是定义,而是相同的基本范围):

amazon:
  Amazon: &Amazon
    - responses:
        default:
          statusCode: '200'
    - passthroughBehavior: when_no_match
    - httpMethod: POST
    - uri: >-
    arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda arn>/invocations
    - credentials: '<role arn>'
    - type: aws     
    - requestTemplates:
      "application/json": "<object definition>"
Run Code Online (Sandbox Code Playgroud)

要使用,我有以下内容:

x-amazon-apigateway-integration:
  *Amazon
Run Code Online (Sandbox Code Playgroud)

API Gateway 导入时收到的错误是“无法解析 API 定义,因为路径 / 中的集成格式错误”

我还尝试在“定义”下定义它,并使用“参考”来访问它:

definitions:
  Amazon:
    type: object
    x-amazon-apigateway-integration:
      responses:
        default:
          statusCode: '200'
      passthroughBehavior: when_no_match
      httpMethod: POST
      uri: >-
          arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda arn>/invocations
      credentials: '<role arn>'
      type: aws     
      requestTemplates:
        "application/json": "<object definition>"   
Run Code Online (Sandbox Code Playgroud)

要使用,我有以下内容:

x-amazon-apigateway-integration:
  $ref: '#/definitions/Amazon'
Run Code Online (Sandbox Code Playgroud)

在导入到 API Gateway 时,我收到以下错误:

由于 Swagger 文件中的错误,您的 API 未导入。

  • 无法为“亚马逊”创建模型:指定的模型无效:验证结果:警告:[],错误:[指定的模型架构无效。不支持的关键字:[“x-amazon-apigateway-integration”]]
  • 此外,还发现了以下警告:
  • “POST /”的未知集成类型“null”。无视。

预先感谢您的帮助。

Woo*_*odz 6

截至 2020 年 4 月 24 日,AWS API Gateway 似乎不支持引用OpenAPI v3 文件中的组件,因为尝试从https://docs.aws.amazon.com/apigateway/latest/developerguide/api-x-amazon-apigateway-integration导入示例gateway-extensions-integrations.html失败并显示

Your API was not imported due to errors in the Swagger file.
Unknown integration type 'null' for 'GET /'. Ignoring.
Unknown integration type 'null' for 'GET /pets'. Ignoring.
Unknown integration type 'null' for 'GET /checkout'. Ignoring.
Run Code Online (Sandbox Code Playgroud)

看来预处理文件是目前避免重复集成定义的唯一选择

  • 不幸的是,这仅支持 HTTP API。更多信息:https://github.com/awsdocs/amazon-api-gateway-developer-guide/issues/65 (4认同)
  • 您是否知道这方面存在一个悬而未决的问题?无法引用集成有点糟糕,并且会线性增加配置量。 (2认同)

Hel*_*len 5

使用 YAML 锚点似乎是个好主意。正确的语法如下。

在 OpenAPI 文件的根级别添加以下内容:

x-definitions:      # <--- "x-" before "definitions" prevents it from being
                    #      attempted to be parsed as an OpenAPI Schema object.
  Amazon:
    type: object
    x-amazon-apigateway-integration: &Amazon   # <--- "&Amazon" is the anchor
      responses:
        default:
          statusCode: '200'
      passthroughBehavior: when_no_match
      httpMethod: POST
      uri: >-
          arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda arn>/invocations
      credentials: '<role arn>'
      type: aws     
      requestTemplates:
        "application/json": "<object definition>" 
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样引用锚点:

x-amazon-apigateway-integration: *Amazon
Run Code Online (Sandbox Code Playgroud)

但是,AWS 解析器可能不支持 YAML 锚点 ( &..., *...)。在这种情况下,您可以尝试使用可以解析 YAML 锚点的解析器来预处理您的定义,然后将解析后的文件提供给 AWS。