OPTIONS 请求的 swagger 路径 $ref

swagger path $ref for OPTIONS requests

我想从 swagger.yml 文件构建 API 网关部署。我需要为所有端点支持 CORS。我的所有 options 路径定义都完全相同。如何在一个地方定义 options 路径,并在我想使用它的地方定义 $ref 路径?

我希望做这样的事情(注意 $ref: '#/definitions/CorsOptions'get 处于同一级别):

---
swagger: "2.0"
info:
  version: "2016-10-26T03:15:31Z"
  title: "corstest"
host: ""
basePath: ""
schemes:
- "https"
paths:
  /page:
    get:
      produces:
      - "application/json"
      responses:
        200:
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
          headers:
            Access-Control-Allow-Origin:
              type: "string"
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Origin: "'*'"
        uri: "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:1234:function:myLambdaFunc/invocations"
        passthroughBehavior: "when_no_match"
        httpMethod: "GET"
        type: "aws_proxy"
    $ref: '#/definitions/CorsOptions'
definitions:
  Empty:
    type: "object"
    title: "Empty Schema"
  CorsOptions:
    options:
      consumes:
      - "application/json"
      produces:
      - "application/json"
      responses:
        200:
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
          headers:
            Access-Control-Allow-Origin:
              type: "string"
            Access-Control-Allow-Methods:
              type: "string"
            Access-Control-Allow-Headers:
              type: "string"
            Cache-Control:
              type: "string"
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
              method.response.header.Access-Control-Allow-Origin: "'*'"
        requestTemplates:
          application/json: "{\"statusCode\": 200}"
        passthroughBehavior: "when_no_match"
        type: "mock"

此 yaml 未通过 swagger validation。我怎样才能完成我正在尝试做的事情,所以我的 yaml 文件不是很大,并且具有相同的 options 路径定义。

swagger spec 支持 $ref 作为 path item object 但是我不知道我可以在哪里放置路径项对象的定义。我认为 API Gateway swagger import 限制从其他地方提取 yaml 文件,但我不是 100% 确定。

我认为这行不通。 API Gateway 目前仅支持模型和模式的 $ref,不支持任意对象。我们有一个积压项在更多地方支持 $ref,因此我们最终可能会添加对此的支持。

如果您想在任意位置指定 $ref 值,可以使用 expand-swagger-refs 模块。

这将采用 Swagger 架构作为输入,并自动内联所有 $ref 值,为您返回适合与 API 网关一起使用的架构。

它在命令行上可用,支持 stdinstdout:

swagger-expand < my-complex-schema.json > aws-compatible.json

并且还作为可导入的 Node 模块:

const { expand, expanded } = require('expand-swagger-refs');
const schema = require('./api/swagger.json');

// Create a copy of the schema, with $ref values expanded: 
const expandedSchema = expanded(schema);

// Or expand the schema object in-place (mutates the object):
expand(schema)