Skip to content

Commit e25c8ae

Browse files
committed
use a default dict in case if a template contains an empty components property in the Open Api definition body instead of failing, as Components property is an optional property in the OpenApi definition.
1 parent 85177ef commit e25c8ae

File tree

6 files changed

+424
-0
lines changed

6 files changed

+424
-0
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ def _openapi_postprocess(self, definition_body):
999999
):
10001000
if definition_body.get("securityDefinitions"):
10011001
components = definition_body.get("components", Py27Dict())
1002+
components = components if components else Py27Dict()
10021003
components["securitySchemes"] = definition_body["securityDefinitions"]
10031004
definition_body["components"] = components
10041005
del definition_body["securityDefinitions"]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Resources:
2+
GetHtmlFunction:
3+
Type: AWS::Serverless::Function
4+
Properties:
5+
CodeUri: s3://sam-demo-bucket/member_portal.zip
6+
Handler: index.handler
7+
Runtime: nodejs12.x
8+
ExplicitApi:
9+
Type: AWS::Serverless::Api
10+
Properties:
11+
StageName: Prod
12+
DefinitionBody:
13+
info:
14+
version: '1.0'
15+
title:
16+
Ref: AWS::StackName
17+
securityDefinitions: # 1 Add security definition
18+
CognitoAuthorizer:
19+
type: "apiKey"
20+
name: "Authorization"
21+
in: "header"
22+
x-amazon-apigateway-authtype: "cognito_user_pools"
23+
x-amazon-apigateway-authorizer:
24+
providerARNs:
25+
- # userPool ARN
26+
type: "cognito_user_pools"
27+
paths:
28+
"/{proxy+}":
29+
x-amazon-apigateway-any-method:
30+
x-amazon-apigateway-integration:
31+
httpMethod: POST
32+
type: aws_proxy
33+
uri:
34+
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHtmlFunction.Arn}/invocations
35+
responses: { }
36+
components:
37+
openapi: '3.0.0'
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
"Resources": {
3+
"GetHtmlFunction": {
4+
"Type": "AWS::Lambda::Function",
5+
"Properties": {
6+
"Code": {
7+
"S3Bucket": "sam-demo-bucket",
8+
"S3Key": "member_portal.zip"
9+
},
10+
"Handler": "index.handler",
11+
"Role": {
12+
"Fn::GetAtt": [
13+
"Arn",
14+
"GetHtmlFunctionRole"
15+
]
16+
},
17+
"Runtime": "nodejs12.x",
18+
"Tags": [
19+
{
20+
"Key": "lambda:createdBy",
21+
"Value": "SAM"
22+
}
23+
]
24+
}
25+
},
26+
"GetHtmlFunctionRole": {
27+
"Type": "AWS::IAM::Role",
28+
"Properties": {
29+
"AssumeRolePolicyDocument": {
30+
"Version": "2012-10-17",
31+
"Statement": [
32+
{
33+
"Action": [
34+
"sts:AssumeRole"
35+
],
36+
"Effect": "Allow",
37+
"Principal": {
38+
"Service": [
39+
"lambda.amazonaws.com"
40+
]
41+
}
42+
}
43+
]
44+
},
45+
"ManagedPolicyArns": [
46+
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
47+
],
48+
"Tags": [
49+
{
50+
"Key": "lambda:createdBy",
51+
"Value": "SAM"
52+
}
53+
]
54+
}
55+
},
56+
"ExplicitApiProdStage": {
57+
"Type": "AWS::ApiGateway::Stage",
58+
"Properties": {
59+
"DeploymentId": {
60+
"Ref": "ExplicitApiDeployment61b3921bb7"
61+
},
62+
"RestApiId": {
63+
"Ref": "ExplicitApi"
64+
},
65+
"StageName": "Prod"
66+
}
67+
},
68+
"ExplicitApiDeployment61b3921bb7": {
69+
"Type": "AWS::ApiGateway::Deployment",
70+
"Properties": {
71+
"Description": "RestApi deployment id: 61b3921bb7522c20a8e0de1d24c974267f3ec17b",
72+
"RestApiId": {
73+
"Ref": "ExplicitApi"
74+
},
75+
"StageName": "Stage"
76+
}
77+
},
78+
"ExplicitApi": {
79+
"Type": "AWS::ApiGateway::RestApi",
80+
"Properties": {
81+
"Body": {
82+
"info": {
83+
"version": "1.0",
84+
"title": {
85+
"Ref": "AWS::StackName"
86+
}
87+
},
88+
"paths": {
89+
"/{proxy+}": {
90+
"x-amazon-apigateway-any-method": {
91+
"x-amazon-apigateway-integration": {
92+
"httpMethod": "POST",
93+
"type": "aws_proxy",
94+
"uri": {
95+
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHtmlFunction.Arn}/invocations"
96+
}
97+
},
98+
"responses": {}
99+
}
100+
}
101+
},
102+
"openapi": "3.0.0",
103+
"components": {
104+
"securitySchemes": {
105+
"CognitoAuthorizer": {
106+
"x-amazon-apigateway-authtype": "cognito_user_pools",
107+
"type": "apiKey",
108+
"name": "Authorization",
109+
"x-amazon-apigateway-authorizer": {
110+
"providerARNs": [
111+
null
112+
],
113+
"type": "cognito_user_pools"
114+
},
115+
"in": "header"
116+
}
117+
}
118+
}
119+
}
120+
}
121+
}
122+
}
123+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"Resources": {
3+
"GetHtmlFunction": {
4+
"Type": "AWS::Lambda::Function",
5+
"Properties": {
6+
"Code": {
7+
"S3Bucket": "sam-demo-bucket",
8+
"S3Key": "member_portal.zip"
9+
},
10+
"Handler": "index.handler",
11+
"Role": {
12+
"Fn::GetAtt": [
13+
"Arn",
14+
"GetHtmlFunctionRole"
15+
]
16+
},
17+
"Runtime": "nodejs12.x",
18+
"Tags": [
19+
{
20+
"Key": "lambda:createdBy",
21+
"Value": "SAM"
22+
}
23+
]
24+
}
25+
},
26+
"GetHtmlFunctionRole": {
27+
"Type": "AWS::IAM::Role",
28+
"Properties": {
29+
"AssumeRolePolicyDocument": {
30+
"Version": "2012-10-17",
31+
"Statement": [
32+
{
33+
"Action": [
34+
"sts:AssumeRole"
35+
],
36+
"Effect": "Allow",
37+
"Principal": {
38+
"Service": [
39+
"lambda.amazonaws.com"
40+
]
41+
}
42+
}
43+
]
44+
},
45+
"ManagedPolicyArns": [
46+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
47+
],
48+
"Tags": [
49+
{
50+
"Key": "lambda:createdBy",
51+
"Value": "SAM"
52+
}
53+
]
54+
}
55+
},
56+
"ExplicitApiProdStage": {
57+
"Type": "AWS::ApiGateway::Stage",
58+
"Properties": {
59+
"DeploymentId": {
60+
"Ref": "ExplicitApiDeployment61b3921bb7"
61+
},
62+
"RestApiId": {
63+
"Ref": "ExplicitApi"
64+
},
65+
"StageName": "Prod"
66+
}
67+
},
68+
"ExplicitApiDeployment61b3921bb7": {
69+
"Type": "AWS::ApiGateway::Deployment",
70+
"Properties": {
71+
"Description": "RestApi deployment id: 61b3921bb7522c20a8e0de1d24c974267f3ec17b",
72+
"RestApiId": {
73+
"Ref": "ExplicitApi"
74+
},
75+
"StageName": "Stage"
76+
}
77+
},
78+
"ExplicitApi": {
79+
"Type": "AWS::ApiGateway::RestApi",
80+
"Properties": {
81+
"Body": {
82+
"info": {
83+
"version": "1.0",
84+
"title": {
85+
"Ref": "AWS::StackName"
86+
}
87+
},
88+
"paths": {
89+
"/{proxy+}": {
90+
"x-amazon-apigateway-any-method": {
91+
"x-amazon-apigateway-integration": {
92+
"httpMethod": "POST",
93+
"type": "aws_proxy",
94+
"uri": {
95+
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHtmlFunction.Arn}/invocations"
96+
}
97+
},
98+
"responses": {}
99+
}
100+
}
101+
},
102+
"openapi": "3.0.0",
103+
"components": {
104+
"securitySchemes": {
105+
"CognitoAuthorizer": {
106+
"x-amazon-apigateway-authtype": "cognito_user_pools",
107+
"type": "apiKey",
108+
"name": "Authorization",
109+
"x-amazon-apigateway-authorizer": {
110+
"providerARNs": [
111+
null
112+
],
113+
"type": "cognito_user_pools"
114+
},
115+
"in": "header"
116+
}
117+
}
118+
}
119+
},
120+
"Parameters": {
121+
"endpointConfigurationTypes": "REGIONAL"
122+
},
123+
"EndpointConfiguration": {
124+
"Types": [
125+
"REGIONAL"
126+
]
127+
}
128+
}
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)