首页 > 解决方案 > AWS API Gateway 将 OAS3 路径和查询参数类型更改为字符串

问题描述

使用OAS3定义创建 API 网关时,路径和查询参数类型将更改为字符串。我找不到任何说明预期行为或原因的 AWS 文档。我知道在手动配置 API Gateway 时它不会公开type路径/查询变量,但是有没有办法让 API Gateway 保留 OAS3路径/查询参数类型以备文档之用?

我们正在尝试使用无服务器开发人员门户来托管我们的 API 文档,并且我们需要公开正确的参数类型;例如,对于limit查询参数,我们需要将其显示为integer.

这是一个基本示例的 SAM。path 参数param是 aninteger并且 swagger 生成的文档正确显示它,但是 API Gateway 将其更改为 astring并且生成的文档将其显示为 a string

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  ComplexityScoreProxy:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
      DefinitionBody:
        openapi: 3.0.1
        info:
          version: "1"
          title: Aws API Gateway Boolean Test
        paths:
          /{param}:
            get:
              x-amazon-apigateway-integration:
                type: http_proxy
                uri: https://example.com
                httpMethod: GET
                responses:
                  "200":
                    statusCode: 200
              parameters:
                - name: param
                  in: path
                  required: true
                  schema:
                    type: integer
              responses:
                "200":
                  description: OK

将 SAM 放入 tempalte.yaml 并运行 cloudformation 进行部署:

aws cloudformation deploy --template-file template.yaml --stack-name boolean-test

获取身份证

aws apigateway get-rest-apis --query "items[?name=='Aws API Gateway Boolean Test'].id"

检索 OAS 用--rest-api-id正确的 id 替换:

aws apigateway get-export --parameters extensions='apigateway' --rest-api-id XXXXXXXXXX --stage-name dev --export-type swagger latestoas.json

导出的 OAS3 定义将参数显示为字符串:

 "paths" : {
    "/{param}" : {
      "get" : {
        "parameters" : [ {
          "name" : "param",
          "in" : "path",
          "required" : true,
          "schema" : {
            "type" : "string"
          }
        } ],

此外,在 Serverless Developer Portal 中查看生成的 swagger 文档会显示paramstring.

标签: swaggeramazon-cloudformationaws-api-gatewayopenapiaws-sam

解决方案


推荐阅读