【问题标题】:How to reuse a string block in swagger如何在招摇中重用字符串块
【发布时间】:2016-11-08 08:20:47
【问题描述】:

我正在为 AWS API Gateway 编写一个 swagger 文件。我必须使用一个文本块来集成每个端点。这就是单个端点当前的样子

'/products/{productId}':
  get:
    tags:
      - product
    summary: Get detailed information about a product
    consumes:
      - application/json
    produces:
      - application/json
    parameters:
      - name: productId
        in: path
        required: true
        type: string
    responses:
      '200':
        description: 200 response
        schema:
          type: array
          items:
            $ref: '#/definitions/product'
      '404':
        description: product not found
        schema:
          type: array
          items:
            $ref: '#/definitions/product'
    x-amazon-apigateway-integration:
      requestTemplates:
        application/json: >
          ##  See
          http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

          ##  This template will pass through all parameters including path,
          querystring, header, stage variables, and context through to the
          integration endpoint via the body/payload

          #set($allParams = $input.params())

          {

          "body-json" : $input.json('$'),

          "params" : {

          #foreach($type in $allParams.keySet())
              #set($params = $allParams.get($type))
          "$type" : {
              #foreach($paramName in $params.keySet())
              "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
                  #if($foreach.hasNext),#end
              #end
          }
              #if($foreach.hasNext),#end
          #end

          },

          "stage-variables" : {

          #foreach($key in $stageVariables.keySet())

          "$key" : "$util.escapeJavaScript($stageVariables.get($key))"
              #if($foreach.hasNext),#end
          #end

          },

          "context" : {
              "account-id" : "$context.identity.accountId",
              "api-id" : "$context.apiId",
              "api-key" : "$context.identity.apiKey",
              "authorizer-principal-id" : "$context.authorizer.principalId",
              "caller" : "$context.identity.caller",
              "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
              "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
              "cognito-identity-id" : "$context.identity.cognitoIdentityId",
              "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
              "http-method" : "$context.httpMethod",
              "stage" : "$context.stage",
              "source-ip" : "$context.identity.sourceIp",
              "user" : "$context.identity.user",
              "user-agent" : "$context.identity.userAgent",
              "user-arn" : "$context.identity.userArn",
              "request-id" : "$context.requestId",
              "resource-id" : "$context.resourceId",
              "resource-path" : "$context.resourcePath"
              }
          }
      uri: >-
        arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:87126xxxxxxx:function:lambdatest_v3/invocations
      passthroughBehavior: never
      responses:
        default:
          statusCode: '200'
      httpMethod: POST
      type: aws

关于 x-amazon-apigateway-integration 的部分需要重复,因为它适用于每条路径。我怎么能不每次都写。是否有可能有一个字符串定义至少包含关于application/json 的部分?

我尝试创建一个字符串定义,但它在 aws import 上不起作用:

definitions:
  MyAPI:
    type: string
    default: >
        #Magic

        #set($allParams = $input.params())

        {

        "body-json" : $input.json('$'),

        "params" : {

        #foreach($type in $allParams.keySet())
            #set($params = $allParams.get($type))
        "$type" : {
            #foreach($paramName in $params.keySet())
            "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
                #if($foreach.hasNext),#end
            #end
        }
            #if($foreach.hasNext),#end
        #end

        },

        "stage-variables" : {

        #foreach($key in $stageVariables.keySet())

        "$key" : "$util.escapeJavaScript($stageVariables.get($key))"
            #if($foreach.hasNext),#end
        #end

        },

        "context" : {
            "account-id" : "$context.identity.accountId",
            "api-id" : "$context.apiId",
            "api-key" : "$context.identity.apiKey",
            "authorizer-principal-id" : "$context.authorizer.principalId",
            "caller" : "$context.identity.caller",
            "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
            "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
            "cognito-identity-id" : "$context.identity.cognitoIdentityId",
            "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
            "http-method" : "$context.httpMethod",
            "stage" : "$context.stage",
            "source-ip" : "$context.identity.sourceIp",
            "user" : "$context.identity.user",
            "user-agent" : "$context.identity.userAgent",
            "user-arn" : "$context.identity.userArn",
            "request-id" : "$context.requestId",
            "resource-id" : "$context.resourceId",
            "resource-path" : "$context.resourcePath"
            }
        }

然后在路径中:

  x-amazon-apigateway-integration:
    requestTemplates:
      application/json: 
        $ref: '#/definitions/MyAPI'
    uri: >-
      arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:8712xxxxxxxx:function:lambdaTest_v3/invocations
    passthroughBehavior: never
    responses:
      default:
        statusCode: '200'
    httpMethod: POST
    type: aws

【问题讨论】:

  • 这可能无法直接回答您的问题,但是您知道您可以从 API Gateway 阶段将其导出为 JSON 吗?
  • 是的,我正在尝试做相反的事情。我有一个版本控制的 swagger 文件,并且我有一个系统可以使用 aws apigateway import-rest-api 将更新到 swagger 文件导入 API 网关。由于将针对所有端点重复网关集成,因此我不想再次重复相同的块。我不知道该怎么做。现在,我在部署过程中添加了集成段落。
  • 啊,好吧,对不起,我误解了你在做什么。我还没有使用 import api 方法,所以我可能无法在这里提供帮助。

标签: amazon-web-services swagger aws-lambda swagger-2.0


【解决方案1】:

http://azimi.me/2015/07/16/split-swagger-into-smaller-files.html

如果您正在编写 Swagger API 规范并且它变得太大,您 可以拆分成多个文件。 Swagger 支持 JSON 参考 (draft) 用于使用远程和本地 JSON 片段来构建一个 大摇大摆的文档。

JSON 参考概述

JSON 引用使用特殊键 $ref 来定义一个“引用” 一段 JSON。例如下面的 JSON 有一个参考 http://example.com/foo.json:

编译分割的 json 文件

一旦您将 json 文件拆分为多个引用,就可以使用 json-refs resolve 命令将它们全部编译在一起。

json-refs resolve -I relative swagger-boot.json > docs/swagger/swaggerInternal.json

【讨论】:

  • 您知道这是否也适用于 AWS API Gateway?上面的规范是为亚马逊 AWS 编写的。只是好奇想做同样的事情。如果可能的话,我们不想在我们这边进行任何字符串操作,因为它会为错误留下机会。
  • 它可能不起作用,因为 API Gateway 将单个文件作为输入 AFAIK。您可以创建对包含重复块的另一个文件的引用。作为构建步骤,您可以使用json-refs。我已经更新了答案来说明。
猜你喜欢
  • 2021-01-13
  • 2019-04-01
  • 2019-06-14
  • 2014-06-11
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
相关资源
最近更新 更多