From c4acdc99e02ba2173231639044d8d68d50075639 Mon Sep 17 00:00:00 2001 From: Gavin Zhang Date: Mon, 17 Apr 2023 11:43:43 -0700 Subject: [PATCH 1/2] fix: Availability Dip Fix for MergeDefinitions property --- samtranslator/model/eventsources/push.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samtranslator/model/eventsources/push.py b/samtranslator/model/eventsources/push.py index c21b9fe9f..b4f7c0af3 100644 --- a/samtranslator/model/eventsources/push.py +++ b/samtranslator/model/eventsources/push.py @@ -971,7 +971,7 @@ def _get_merged_definitions( - otherwise include key-value pairs from both definitions """ merged_definition_body = source_definition_body.copy() - source_body_paths = merged_definition_body.get("paths", {}) + source_body_paths = merged_definition_body.get("paths") or {} try: path_method_body = dict_deep_get(source_body_paths, [self.Path, self.Method]) or {} From af0aa24cb02271cee79035a77d0ae853337d4fa5 Mon Sep 17 00:00:00 2001 From: Gavin Zhang Date: Mon, 17 Apr 2023 12:02:09 -0700 Subject: [PATCH 2/2] Add tests --- ...api_with_merge_definitions_null_paths.yaml | 26 ++++ ...api_with_merge_definitions_null_paths.json | 112 ++++++++++++++++ ...api_with_merge_definitions_null_paths.json | 120 ++++++++++++++++++ ...api_with_merge_definitions_null_paths.json | 120 ++++++++++++++++++ 4 files changed, 378 insertions(+) create mode 100644 tests/translator/input/api_with_merge_definitions_null_paths.yaml create mode 100644 tests/translator/output/api_with_merge_definitions_null_paths.json create mode 100644 tests/translator/output/aws-cn/api_with_merge_definitions_null_paths.json create mode 100644 tests/translator/output/aws-us-gov/api_with_merge_definitions_null_paths.json diff --git a/tests/translator/input/api_with_merge_definitions_null_paths.yaml b/tests/translator/input/api_with_merge_definitions_null_paths.yaml new file mode 100644 index 000000000..797eac94c --- /dev/null +++ b/tests/translator/input/api_with_merge_definitions_null_paths.yaml @@ -0,0 +1,26 @@ +Resources: + WebhooksApi: + Type: AWS::Serverless::Api + Properties: + StageName: live + MergeDefinitions: true + DefinitionBody: + swagger: 2 + paths: + x-amazon-apigateway-policy: + Version: '2012-10-17' + + + WebhooksReceiver: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://bucket/key + Handler: code/handler + Runtime: python3.8 + Events: + AllEvent: + Type: Api + Properties: + RestApiId: !Ref WebhooksApi + Path: /proxy + Method: any diff --git a/tests/translator/output/api_with_merge_definitions_null_paths.json b/tests/translator/output/api_with_merge_definitions_null_paths.json new file mode 100644 index 000000000..3cddd4eef --- /dev/null +++ b/tests/translator/output/api_with_merge_definitions_null_paths.json @@ -0,0 +1,112 @@ +{ + "Resources": { + "WebhooksApi": { + "Properties": { + "Body": { + "paths": null, + "swagger": 2, + "x-amazon-apigateway-policy": { + "Version": "2012-10-17" + } + } + }, + "Type": "AWS::ApiGateway::RestApi" + }, + "WebhooksApiDeployment5ce813cc52": { + "Properties": { + "Description": "RestApi deployment id: 5ce813cc52a4e5d017317029559e0c18939085eb", + "RestApiId": { + "Ref": "WebhooksApi" + }, + "StageName": "Stage" + }, + "Type": "AWS::ApiGateway::Deployment" + }, + "WebhooksApiliveStage": { + "Properties": { + "DeploymentId": { + "Ref": "WebhooksApiDeployment5ce813cc52" + }, + "RestApiId": { + "Ref": "WebhooksApi" + }, + "StageName": "live" + }, + "Type": "AWS::ApiGateway::Stage" + }, + "WebhooksReceiver": { + "Properties": { + "Code": { + "S3Bucket": "bucket", + "S3Key": "key" + }, + "Handler": "code/handler", + "Role": { + "Fn::GetAtt": [ + "WebhooksReceiverRole", + "Arn" + ] + }, + "Runtime": "python3.8", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::Lambda::Function" + }, + "WebhooksReceiverAllEventPermissionlive": { + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Ref": "WebhooksReceiver" + }, + "Principal": "apigateway.amazonaws.com", + "SourceArn": { + "Fn::Sub": [ + "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/*/proxy", + { + "__ApiId__": { + "Ref": "WebhooksApi" + }, + "__Stage__": "*" + } + ] + } + }, + "Type": "AWS::Lambda::Permission" + }, + "WebhooksReceiverRole": { + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::IAM::Role" + } + } +} diff --git a/tests/translator/output/aws-cn/api_with_merge_definitions_null_paths.json b/tests/translator/output/aws-cn/api_with_merge_definitions_null_paths.json new file mode 100644 index 000000000..0a754aa07 --- /dev/null +++ b/tests/translator/output/aws-cn/api_with_merge_definitions_null_paths.json @@ -0,0 +1,120 @@ +{ + "Resources": { + "WebhooksApi": { + "Properties": { + "Body": { + "paths": null, + "swagger": 2, + "x-amazon-apigateway-policy": { + "Version": "2012-10-17" + } + }, + "EndpointConfiguration": { + "Types": [ + "REGIONAL" + ] + }, + "Parameters": { + "endpointConfigurationTypes": "REGIONAL" + } + }, + "Type": "AWS::ApiGateway::RestApi" + }, + "WebhooksApiDeployment5ce813cc52": { + "Properties": { + "Description": "RestApi deployment id: 5ce813cc52a4e5d017317029559e0c18939085eb", + "RestApiId": { + "Ref": "WebhooksApi" + }, + "StageName": "Stage" + }, + "Type": "AWS::ApiGateway::Deployment" + }, + "WebhooksApiliveStage": { + "Properties": { + "DeploymentId": { + "Ref": "WebhooksApiDeployment5ce813cc52" + }, + "RestApiId": { + "Ref": "WebhooksApi" + }, + "StageName": "live" + }, + "Type": "AWS::ApiGateway::Stage" + }, + "WebhooksReceiver": { + "Properties": { + "Code": { + "S3Bucket": "bucket", + "S3Key": "key" + }, + "Handler": "code/handler", + "Role": { + "Fn::GetAtt": [ + "WebhooksReceiverRole", + "Arn" + ] + }, + "Runtime": "python3.8", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::Lambda::Function" + }, + "WebhooksReceiverAllEventPermissionlive": { + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Ref": "WebhooksReceiver" + }, + "Principal": "apigateway.amazonaws.com", + "SourceArn": { + "Fn::Sub": [ + "arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/*/proxy", + { + "__ApiId__": { + "Ref": "WebhooksApi" + }, + "__Stage__": "*" + } + ] + } + }, + "Type": "AWS::Lambda::Permission" + }, + "WebhooksReceiverRole": { + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::IAM::Role" + } + } +} diff --git a/tests/translator/output/aws-us-gov/api_with_merge_definitions_null_paths.json b/tests/translator/output/aws-us-gov/api_with_merge_definitions_null_paths.json new file mode 100644 index 000000000..9da42fe51 --- /dev/null +++ b/tests/translator/output/aws-us-gov/api_with_merge_definitions_null_paths.json @@ -0,0 +1,120 @@ +{ + "Resources": { + "WebhooksApi": { + "Properties": { + "Body": { + "paths": null, + "swagger": 2, + "x-amazon-apigateway-policy": { + "Version": "2012-10-17" + } + }, + "EndpointConfiguration": { + "Types": [ + "REGIONAL" + ] + }, + "Parameters": { + "endpointConfigurationTypes": "REGIONAL" + } + }, + "Type": "AWS::ApiGateway::RestApi" + }, + "WebhooksApiDeployment5ce813cc52": { + "Properties": { + "Description": "RestApi deployment id: 5ce813cc52a4e5d017317029559e0c18939085eb", + "RestApiId": { + "Ref": "WebhooksApi" + }, + "StageName": "Stage" + }, + "Type": "AWS::ApiGateway::Deployment" + }, + "WebhooksApiliveStage": { + "Properties": { + "DeploymentId": { + "Ref": "WebhooksApiDeployment5ce813cc52" + }, + "RestApiId": { + "Ref": "WebhooksApi" + }, + "StageName": "live" + }, + "Type": "AWS::ApiGateway::Stage" + }, + "WebhooksReceiver": { + "Properties": { + "Code": { + "S3Bucket": "bucket", + "S3Key": "key" + }, + "Handler": "code/handler", + "Role": { + "Fn::GetAtt": [ + "WebhooksReceiverRole", + "Arn" + ] + }, + "Runtime": "python3.8", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::Lambda::Function" + }, + "WebhooksReceiverAllEventPermissionlive": { + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Ref": "WebhooksReceiver" + }, + "Principal": "apigateway.amazonaws.com", + "SourceArn": { + "Fn::Sub": [ + "arn:aws-us-gov:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/*/proxy", + { + "__ApiId__": { + "Ref": "WebhooksApi" + }, + "__Stage__": "*" + } + ] + } + }, + "Type": "AWS::Lambda::Permission" + }, + "WebhooksReceiverRole": { + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::IAM::Role" + } + } +}