首页 > 解决方案 > API 网关的“导出为 Swagger”和“导出为 Swagger + Postman”缺少“参数”对象

问题描述

我有一个 API 网关端点 (/item/{itemId}),其中有一个路径参数“itemId”。这是我用来在 API 网关中创建端点的招摇定义。

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "sample-postman-import"
  },
  "host": "example.com",
  "basePath": "/api",
  "schemes": ["https"],
  "paths": {
    "/item/{itemId}": {
      "get": {
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "itemId",
            "in": "path",
            "required": true,
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "200 response"
            }
          }
      }
    }
  }

部署 API 后,当我将其导出为 swagger 定义时,导出的定义缺少“参数”对象,使其成为不完整的 swagger 文件。当我看到同样的问题时

  1. 尝试从 UI 导出,如https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-export-api.html所示
  2. 使用来自 javascript aws-sdk https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#getExport-property的 getExport API 调用

这是导出的样子:

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "sample-postman-import_old"
  },
  "host": "example.com",
  "basePath": "/api",
  "schemes": ["https"],
  "paths": {
    "/item/{itemId}": {
      "get": {
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "200 response"
            }
          }
      }
    }
  }
}

标签: amazon-web-servicesexportpostmanaws-api-gatewayswagger-2.0

解决方案


当我使用 Terraform 创建 API 网关时,我遇到了同样的问题。在 AWS UI 上显示了参数,但是当我通过 AWS CLI 使用aws apigateway get-method命令请求方法信息时,未提供参数。因此,请确保根据需要设置了“requestParameters”,如果没有,请通过 CLI 或 Terraform 进行设置。


推荐阅读