7

I am trying to write a Swagger config for the AWS API Gateway deployment, and came up with this for a sample (mostly copied from the documentation):

{
  "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'"
              }
            }
          }
        }
      }
    }
  }
}

But the problem is that

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

Outputs the following:

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]

That part is taken directly from the documentation, so this is really confusing to me. Any ideas what to do? Documentation seems lacking in many ways, so it is hard to solve many problems with it.

1
  • Please mark the answer if it helps solve the problem
    – Efren
    Commented Jun 27, 2018 at 23:48

1 Answer 1

7

Have you tried defining version and vendor as parameters to the method?

{
  "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 Gateway requires method request parameters to be defined before being referenced

2
  • Thank you so much for this answer: it has saved my day! :) The point I got stuck on was in the "responses" under "paths", the mapping must be "headerS" rather than "header" like it is under "responseParameters" (method.response.header.xxx). Sheesh, I wish there were examples with Swagger OpenAPI and CloudFormation together
    – Tracy Xia
    Commented Oct 23, 2019 at 15:51
  • Thank you for the hint, I'm not working with Swagger but I was trying to set up an API Gateway Method for an HTTP proxy with CloudFormation, and it complained with the same error - explicitly defining the RequestParameters (see docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…) solved the issue.
    – dwytrykus
    Commented Jan 14, 2020 at 15:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.