【问题标题】:Invalid mapping expression parameter specified指定的映射表达式参数无效
【发布时间】:2018-04-04 03:09:57
【问题描述】:

我正在尝试为 AWS API Gateway 部署编写一个 Swagger 配置,并想出了一个示例(主要是从文档中复制的):

{
  "swagger": "2.0",
  "info": {
    "description": "desc",
    "title": "TestAPI",
    "version": "1.0"
  },
  "schemes": [
    "https"
  ],
  "paths": {
    "/": {
      "get": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "test",
            "headers": {
              "Content-type": {
                "type": "string"
              }
            }
          }
        },
        "x-amazon-apigateway-integration" : {
          "type" : "aws",
          "uri" : "[REDACTED]",
          "credentials" : "[REDACTED]",
          "httpMethod" : "POST",
          "requestTemplates" : {
            "application/json" : "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }"
          },
          "requestParameters" : {
            "integration.request.querystring.stage" : "method.request.querystring.version",
            "integration.request.querystring.provider" : "method.request.querystring.vendor"
          },
          "responses" : {
            "2\\d{2}" : {
              "statusCode" : "200",
              "responseParameters" : {
                "method.response.header.requestId" : "integration.response.header.cid"
              },
              "responseTemplates" : {
                "application/json" : "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }"
              }
            },
            "302" : {
              "statusCode" : "302",
              "responseParameters" : {
                "method.response.header.Location" : "integration.response.body.redirect.url"
              }
            },
            "default" : {
              "statusCode" : "400",
              "responseParameters" : {
                "method.response.header.test-method-response-header" : "'static value'"
              }
            }
          }
        }
      }
    }
  }
}

但问题是

aws apigateway import-rest-api --body 'file://deploy/api.json' --region eu-west-1 

输出以下内容:

An error occurred (BadRequestException) when calling the ImportRestApi operation: Errors found during import:
    Unable to put integration on 'GET' for resource at path '/': Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression parameter specified: method.request.querystring.version]

那部分直接取自documentation,所以这真的让我很困惑。有什么想法该怎么做?文档似乎在很多方面都缺乏,因此很难用它来解决很多问题。

【问题讨论】:

  • 如果对解决问题有帮助请标记答案

标签: aws-api-gateway


【解决方案1】:

您是否尝试过将versionvendor 定义为方法的参数?

{
  "swagger": "2.0",
  "info": {
    "description": "desc",
    "title": "TestAPI",
    "version": "1.0"
  },
  "schemes": [
    "https"
  ],
  "paths": {
    "/": {
      "get": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters" : [
            {
                "name" : "version",
                "in" : "query",
                "required" : true,
                "type" : "string"
                "description" : "The version requested"
            },          
            {
                "name" : "vendor",
                "in" : "query",
                "required" : true,
                "type" : "string"
                "description" : "The vendor being queried"
            }
        ],
        "responses": {
          "200": {
            "description": "test",
            "headers": {
              "Content-type": {
                "type": "string"
              }
            }
          }
        },
        "x-amazon-apigateway-integration" : {
          "type" : "aws",
          "uri" : "[REDACTED]",
          "credentials" : "[REDACTED]",
          "httpMethod" : "POST",
          "requestTemplates" : {
            "application/json" : "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }"
          },
          "requestParameters" : {
            "integration.request.querystring.stage" : "method.request.querystring.version",
            "integration.request.querystring.provider" : "method.request.querystring.vendor"
          },
          "responses" : {
            "2\\d{2}" : {
              "statusCode" : "200",
              "responseParameters" : {
                "method.response.header.requestId" : "integration.response.header.cid"
              },
              "responseTemplates" : {
                "application/json" : "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }"
              }
            },
            "302" : {
              "statusCode" : "302",
              "responseParameters" : {
                "method.response.header.Location" : "integration.response.body.redirect.url"
              }
            },
            "default" : {
              "statusCode" : "400",
              "responseParameters" : {
                "method.response.header.test-method-response-header" : "'static value'"
              }
            }
          }
        }
      }
    }
  }
}

API网关要求方法请求参数为defined before being referenced

【讨论】:

  • 非常感谢您的回答:它拯救了我的一天! :) 我坚持的一点是在“paths”下的“responses”中,映射必须是“headerS”而不是“header”,就像它在“responseParameters”(method.response.header.xxx)下一样。哎呀,我希望有 Swagger OpenAPI 和 CloudFormation 一起使用的示例
  • 感谢您的提示,我没有使用 Swagger,但我尝试使用 CloudFormation 为 HTTP 代理设置 API 网关方法,但它抱怨同样的错误 - 明确定义RequestParameters(见docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…)解决了这个问题。
猜你喜欢
  • 2016-07-07
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 2013-05-08
相关资源
最近更新 更多