如何在整个 Swagger YAML 文档中重复使用我的 x-amazon-apigateway-integration 定义? [英] How to re-use my x-amazon-apigateway-integration definition throughout Swagger YAML document?

查看:21
本文介绍了如何在整个 Swagger YAML 文档中重复使用我的 x-amazon-apigateway-integration 定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently using Swagger to define an API with many end-points, and each one of those end-points all have the same definition for the 'x-amazon-apigateway-integration' key. I would like to define this somewhere in the document, and re-use that definition through-out.

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

Either I am not understanding how the definition should be defined, I am not placing it in the correct location or a mix of the two. I have tried defining this definition within 'definitions', and as some alias under it's own key. The definition (with key information removed) is:

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>"  

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

I have tried defining this as an alias under it's own key (not definitions, but the same base scope):

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>"

要使用,我有以下内容:

To use, I have the following:

x-amazon-apigateway-integration:
  *Amazon

API Gateway 导入时收到的错误是由于路径/处的集成格式错误,无法解析 API 定义"

The error received on API Gateway import is 'Unable to parse API definition because of a malformed integration at path /'

我也尝试在定义"下定义它,并使用参考"来访问它:

I have also tried defining this under 'definitions', and using 'ref' to access it:

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>"   

要使用,我有以下内容:

To use, I have the following:

x-amazon-apigateway-integration:
  $ref: '#/definitions/Amazon'

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

On import to API Gateway I receive the following error(s):

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

Your API was not imported due to errors in the Swagger file.

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

提前感谢您的帮助.

推荐答案

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

Using YAML anchors seems like a good idea. The correct syntax is as follows.

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

Add the following on the root level of your OpenAPI file:

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>" 

那么你可以这样引用锚点:

Then you can refer to the anchor like this:

x-amazon-apigateway-integration: *Amazon

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

However, it might be that AWS parser does not support YAML anchors (&..., *...). In that case you can try pre-processing your definition using a parser that can resolve YAML anchors and then feed the resolved file to AWS.

这篇关于如何在整个 Swagger YAML 文档中重复使用我的 x-amazon-apigateway-integration 定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆