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

查看:84
本文介绍了如何在整个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网关导入时收到的错误是由于路径/集成格式错误,无法解析API定义"

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

我也尝试过在'definitions'下定义它,并使用'ref'访问它:

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.

  • 无法为"Amazon"创建模型:指定了无效的模型:验证结果:警告:[],错误:[指定了无效的模型架构.不支持的关键字:["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: &Amazon   # <--- "&Amazon" is the anchor
    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>" 

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

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天全站免登陆