From 1331313282cc499ae658f86572eb49123e1999d3 Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Fri, 7 Jul 2023 09:31:51 +0200 Subject: [PATCH] fix(apigateway): method response from rest api default options are not passed to Method --- .../aws-cdk-lib/aws-apigateway/lib/method.ts | 2 +- .../aws-apigateway/test/method.test.ts | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-apigateway/lib/method.ts b/packages/aws-cdk-lib/aws-apigateway/lib/method.ts index 2a1acb7202ec7..db93c7ffef611 100644 --- a/packages/aws-cdk-lib/aws-apigateway/lib/method.ts +++ b/packages/aws-cdk-lib/aws-apigateway/lib/method.ts @@ -203,7 +203,7 @@ export class Method extends Resource { authorizer._attachToApi(this.api); } - this.methodResponses = options.methodResponses ?? []; + this.methodResponses = options.methodResponses ?? defaultMethodOptions.methodResponses ?? []; const integration = props.integration ?? this.resource.defaultIntegration ?? new MockIntegration(); const bindResult = integration.bind(this); diff --git a/packages/aws-cdk-lib/aws-apigateway/test/method.test.ts b/packages/aws-cdk-lib/aws-apigateway/test/method.test.ts index 17ad07afbb9fe..e5e1a0855c3d5 100644 --- a/packages/aws-cdk-lib/aws-apigateway/test/method.test.ts +++ b/packages/aws-cdk-lib/aws-apigateway/test/method.test.ts @@ -909,6 +909,38 @@ describe('method', () => { }); + test('methodResponse should be passed from defaultMethodOptions', () => { + // GIVEN + const stack = new cdk.Stack(); + const api = new apigw.RestApi(stack, 'test-api', { + cloudWatchRole: false, + deploy: false, + defaultMethodOptions: { + requestParameters: { 'method.request.path.proxy': true }, + methodResponses: [ + { + statusCode: '200', + }, + ], + }, + }); + + // WHEN + new apigw.Method(stack, 'method-man', { + httpMethod: 'GET', + resource: api.root, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Method', { + HttpMethod: 'GET', + MethodResponses: [{ + StatusCode: '200', + }], + }); + + }); + describe('Metrics', () => { test('metric', () => { // GIVEN