1
votes

I'm using a swagger/OpenAPI file to describe a mock API that I've implemented in AWS API Gateway. The API returns a HTTP 200 when the swagger is written like this:

"x-amazon-apigateway-request-validator": "params-only",
"x-amazon-apigateway-integration": {
    "requestTemplates": {
        "application/json" : "{\"statusCode\": 200 }"
    },
    "responses" : {
        "2\\d{2}" : {
            "statusCode" : "200"
        },
        "4\\d{2}" : {
            "statusCode" : "400"
        }
    },
    "passthroughBehavior": "when_no_match",
    "timeoutInMillis": "20000",
    "httpMethod": "POST",
    "type": "mock"
}

However, when I alter the swagger to return a value found in the request header, I get a HTTP 500 error. Here's the modified swagger:

"x-amazon-apigateway-request-validator": "params-only",
"x-amazon-apigateway-integration": {
    "requestTemplates": {
        "application/json" : "{\"statusCode\": 200 }"
    },
    "responses" : {
        "2\\d{2}" : {
            "statusCode" : "$input.params('Mock-Return-Code')"
        },
        "4\\d{2}" : {
            "statusCode" : "$input.params('Mock-Return-Code')"
        }
    },
    "passthroughBehavior": "when_no_match",
    "timeoutInMillis": "20000",
    "httpMethod": "POST",
    "type": "mock"
}

Here's the exact error I receive when I run the API with the $input.params reference:

Execution failed due to configuration error: Output mapping refers to an invalid method response: $input.params('Mock-Return-Code')

Any ideas?

1

1 Answers

1
votes

I figured out that I was going about this the wrong way. Instead of adjusting the response template, I should have been adjusting the request template. Here's an example of what I got working.

First, I had to update my path parameters so that I declare the optional header.

"get": {
    "parameters": [
        {
            "name": "x-mock-response-code",
            "in": "header",
            "required": false,
            "type": "string",
            "default": "200"
        }
    ]
}

Next, I modified the API Gateway Integrations to use the header value to determine the response template to return.

"x-amazon-apigateway-request-validator": "params-only",
"x-amazon-apigateway-integration": {
    "requestTemplates": {
        "application/json": "#set($rc = $input.params('x-mock-response-code'))\r\n\r\n#if($rc.length() != 0)\r\n {\r\n \"statusCode\" : $rc\r\n }\r\n#else\r\n {\r\n \"statusCode\" : 200\r\n }\r\n#end"
    },
    "requestParameters" : {
        "integration.request.header.x-mock-response-code" : "method.request.header.x-mock-response-code"
    },
    "responses" : {
        "2\\d{2}" : {
            "statusCode" : "200"
        },
        "4\\d{2}" : {
            "statusCode" : "400"
        }
    },
    "passthroughBehavior": "when_no_match",
    "timeoutInMillis": "20000",
    "httpMethod": "POST",
    "type": "mock"
}

The following shows the JSON request template code in pretty format:

#set($rc = $input.params('x-mock-response-code'))

#if($rc.length() != 0)
{
    "statusCode" : $rc
}
#else
{
    "statusCode" : 200
}
#end