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

fix: accept empty components property in Open Api Definition #2296

Merged
merged 2 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ def _openapi_postprocess(self, definition_body):
):
if definition_body.get("securityDefinitions"):
components = definition_body.get("components", Py27Dict())
components = components if components else Py27Dict()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't second parameter of get is the default value if the item is None? If so, the line 1002 would be redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, get will only return default value if the components is not the dict at all, but if it is in the dict and its value is None, it will return None and not the default value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, can we add a comment next to it so it won't be removed accidentally in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

components["securitySchemes"] = definition_body["securityDefinitions"]
definition_body["components"] = components
del definition_body["securityDefinitions"]
Expand Down
37 changes: 37 additions & 0 deletions tests/translator/input/api_with_none_components.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Resources:
GetHtmlFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/member_portal.zip
Handler: index.handler
Runtime: nodejs12.x
ExplicitApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
DefinitionBody:
info:
version: '1.0'
title:
Ref: AWS::StackName
securityDefinitions: # 1 Add security definition
CognitoAuthorizer:
type: "apiKey"
name: "Authorization"
in: "header"
x-amazon-apigateway-authtype: "cognito_user_pools"
x-amazon-apigateway-authorizer:
providerARNs:
- # userPool ARN
type: "cognito_user_pools"
paths:
"/{proxy+}":
x-amazon-apigateway-any-method:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHtmlFunction.Arn}/invocations
responses: { }
components:
openapi: '3.0.0'
123 changes: 123 additions & 0 deletions tests/translator/output/api_with_none_components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"Resources": {
"GetHtmlFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "member_portal.zip"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"Arn",
"GetHtmlFunctionRole"
]
},
"Runtime": "nodejs12.x",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"GetHtmlFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"ExplicitApiProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ExplicitApiDeployment61b3921bb7"
},
"RestApiId": {
"Ref": "ExplicitApi"
},
"StageName": "Prod"
}
},
"ExplicitApiDeployment61b3921bb7": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"Description": "RestApi deployment id: 61b3921bb7522c20a8e0de1d24c974267f3ec17b",
"RestApiId": {
"Ref": "ExplicitApi"
},
"StageName": "Stage"
}
},
"ExplicitApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"info": {
"version": "1.0",
"title": {
"Ref": "AWS::StackName"
}
},
"paths": {
"/{proxy+}": {
"x-amazon-apigateway-any-method": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHtmlFunction.Arn}/invocations"
}
},
"responses": {}
}
}
},
"openapi": "3.0.0",
"components": {
"securitySchemes": {
"CognitoAuthorizer": {
"x-amazon-apigateway-authtype": "cognito_user_pools",
"type": "apiKey",
"name": "Authorization",
"x-amazon-apigateway-authorizer": {
"providerARNs": [
null
],
"type": "cognito_user_pools"
},
"in": "header"
}
}
}
}
}
}
}
}
131 changes: 131 additions & 0 deletions tests/translator/output/aws-cn/api_with_none_components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"Resources": {
"GetHtmlFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "member_portal.zip"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"Arn",
"GetHtmlFunctionRole"
]
},
"Runtime": "nodejs12.x",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"GetHtmlFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"ExplicitApiProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ExplicitApiDeployment61b3921bb7"
},
"RestApiId": {
"Ref": "ExplicitApi"
},
"StageName": "Prod"
}
},
"ExplicitApiDeployment61b3921bb7": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"Description": "RestApi deployment id: 61b3921bb7522c20a8e0de1d24c974267f3ec17b",
"RestApiId": {
"Ref": "ExplicitApi"
},
"StageName": "Stage"
}
},
"ExplicitApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"info": {
"version": "1.0",
"title": {
"Ref": "AWS::StackName"
}
},
"paths": {
"/{proxy+}": {
"x-amazon-apigateway-any-method": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHtmlFunction.Arn}/invocations"
}
},
"responses": {}
}
}
},
"openapi": "3.0.0",
"components": {
"securitySchemes": {
"CognitoAuthorizer": {
"x-amazon-apigateway-authtype": "cognito_user_pools",
"type": "apiKey",
"name": "Authorization",
"x-amazon-apigateway-authorizer": {
"providerARNs": [
null
],
"type": "cognito_user_pools"
},
"in": "header"
}
}
}
},
"Parameters": {
"endpointConfigurationTypes": "REGIONAL"
},
"EndpointConfiguration": {
"Types": [
"REGIONAL"
]
}
}
}
}
}