Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
commiterate opened this issue Jul 6, 2023 · 2 comments · Fixed by #26275
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md p2

Comments

@commiterate
Copy link

commiterate commented Jul 6, 2023

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"
				}
			]
		}
	}
}
@commiterate commiterate added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 6, 2023
@github-actions github-actions bot added the @aws-cdk/aws-apigateway Related to Amazon API Gateway label Jul 6, 2023
@commiterate commiterate changed the title (aws_apigateway): Method responses from default method options set on RestApi not passed to Methods (aws-apigateway): Method responses from default method options set on RestApi not passed to Methods Jul 6, 2023
@commiterate commiterate changed the title (aws-apigateway): Method responses from default method options set on RestApi not passed to Methods aws_apigateway: Method responses from default method options set on RestApi not passed to Methods Jul 6, 2023
@peterwoodworth
Copy link
Contributor

This line is probably the issue

this.methodResponses = options.methodResponses ?? [];

Think it should be this.methodResponses = options.methodResponses ?? this.defaultMethodOptions.methodResponses ?? [];

@peterwoodworth peterwoodworth added good first issue Related to contributions. See CONTRIBUTING.md p2 effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jul 6, 2023
mrgrain added a commit to lpizzinidev/aws-cdk that referenced this issue Jul 7, 2023
mergify bot added a commit to lpizzinidev/aws-cdk that referenced this issue Jul 7, 2023
@mergify mergify bot closed this as completed in #26275 Jul 7, 2023
mergify bot pushed a commit that referenced this issue Jul 7, 2023
…t passed to Method (#26275)

`methodResponses` specified in `RestApi`'s `defaultMethodOptions` were not passed to `Method` as default.
This fix solves the problem.

Closes #26252.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

github-actions bot commented Jul 7, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

tmokmss pushed a commit to tmokmss/aws-cdk that referenced this issue Jul 9, 2023
…t passed to Method (aws#26275)

`methodResponses` specified in `RestApi`'s `defaultMethodOptions` were not passed to `Method` as default.
This fix solves the problem.

Closes aws#26252.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
bmoffatt pushed a commit to bmoffatt/aws-cdk that referenced this issue Jul 29, 2023
…t passed to Method (aws#26275)

`methodResponses` specified in `RestApi`'s `defaultMethodOptions` were not passed to `Method` as default.
This fix solves the problem.

Closes aws#26252.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md p2
Projects
None yet
2 participants