Skip to content

aws_apigateway: Method responses from default method options set on RestApi not passed to Methods #26252

@commiterate

Description

@commiterate

Describe the bug

When creating a RestApi and setting RestApiProps.defaultMethodOptions.methodResponses, the method responses aren't passed to methods created on the RestApi that don't have a MethodOptions passed in.

Expected Behavior

Method responses should be passed to methods.

Current Behavior

Method responses aren't passed to methods. Other properties like RestApiProps.defaultMethodOptions.authorizationType seem to be passed though.

Reproduction Steps

TypeScript code:

import { Construct, aws_apigateway } from "aws-cdk-lib";

export class MockApiGatewayRestApi extends Construct {
	constructor(scope: Construct, id: string) {
		super(scope, id);

		const restApi = new aws_apigateway.RestApi(this, "REST_API", {
			defaultIntegration: new aws_apigateway.MockIntegration({
				passthroughBehavior: aws_apigateway.PassthroughBehavior.NEVER,
				requestTemplates: {
					"application/json": '{"statusCode": 200}'
				},
				integrationResponses: [
					{
						statusCode: "200"
					}
				]
			}),
			defaultMethodOptions: {
				authorizationType: aws_apigateway.AuthorizationType.IAM,
				methodResponses: [
					{
						statusCode: "200"
					}
				]
			}
		});

		restApi.root.addResource("test").addMethod("GET");
	}
}

Resulting AWS::ApiGateway::Method:

{
	"{Logical ID}": {
		"Type": "AWS::ApiGateway::Method",
		"Properties": {
			"HttpMethod": "GET",
			"ResourceId": {
				"Ref": "{AWS::ApiGateway::Resource logical ID for test resource}"
			},
			"RestApiId": {
				"Ref": "{AWS::ApiGateway::RestApi logical ID for endpoint}"
			},
			"AuthorizationType": "AWS_IAM",
			"Integration": {
				"IntegrationResponses": [
					{
						"StatusCode": "200"
					}
				],
				"PassthroughBehavior": "NEVER",
				"RequestTemplates": {
					"application/json": "{\"statusCode\": 200}"
				},
				"Type": "MOCK"
			}
		}
	}
}

CDK CLI Version

2.85.0

Framework Version

2.85.0

Node.js Version

20.2.0

OS

macOS Monterey 12.6.6 and Amazon Linux 2

Language

Typescript

Language Version

5.1.3

Other information

If we pass in the integration and method options when creating the method, the resulting template is as expected.

TypeScript code:

import { Construct, aws_apigateway } from "aws-cdk-lib";

export class MockApiGatewayRestApi extends Construct {
	constructor(scope: Construct, id: string) {
		super(scope, id);

		const restApi = new aws_apigateway.RestApi(this, "REST_API");

		const mockIntegration = new aws_apigateway.MockIntegration({
			passthroughBehavior: aws_apigateway.PassthroughBehavior.NEVER,
			requestTemplates: {
				"application/json": '{"statusCode": 200}'
			},
			integrationResponses: [
				{
					statusCode: "200"
				}
			]
		});

		const methodOptions: aws_apigateway.MethodOptions = {
			authorizationType: aws_apigateway.AuthorizationType.IAM,
			methodResponses: [
				{
					statusCode: "200"
				}
			]
		};
		
		restApi.root.addResource("test").addMethod("GET", mockIntegration, methodOptions);
	}
}

Resulting AWS::ApiGateway::Method:

{
	"{Logical ID}": {
		"Type": "AWS::ApiGateway::Method",
		"Properties": {
			"HttpMethod": "GET",
			"ResourceId": {
				"Ref": "{AWS::ApiGateway::Resource logical ID for test resource}"
			},
			"RestApiId": {
				"Ref": "{AWS::ApiGateway::RestApi logical ID for endpoint}"
			},
			"AuthorizationType": "AWS_IAM",
			"Integration": {
				"IntegrationResponses": [
					{
						"StatusCode": "200"
					}
				],
				"PassthroughBehavior": "NEVER",
				"RequestTemplates": {
					"application/json": "{\"statusCode\": 200}"
				},
				"Type": "MOCK"
			},
			"MethodResponses": [
				{
					"StatusCode": "200"
				}
			]
		}
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-apigatewayRelated to Amazon API GatewaybugThis issue is a bug.effort/smallSmall work item – less than a day of effortgood first issueRelated to contributions. See CONTRIBUTING.mdp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions