Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,12 @@ def _add_openapi_integration(self, api, function, manage_swagger=False):
self._add_auth_to_openapi_integration(api, editor)
if self.TimeoutInMillis:
editor.add_timeout_to_method(api=api, path=self.Path, method_name=self.Method, timeout=self.TimeoutInMillis)
path_parameters = re.findall("{(.*?)}", self.Path)
if path_parameters:
editor.add_path_parameters_to_method(
api=api, path=self.Path, method_name=self.Method, path_parameters=path_parameters
)

api["DefinitionBody"] = editor.openapi

def _add_auth_to_openapi_integration(self, api, editor):
Expand Down
32 changes: 32 additions & 0 deletions samtranslator/open_api/open_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,38 @@ def add_timeout_to_method(self, api, path, method_name, timeout):
if self.method_definition_has_integration(method_definition):
method_definition[self._X_APIGW_INTEGRATION]["timeoutInMillis"] = timeout

def add_path_parameters_to_method(self, api, path, method_name, path_parameters):
"""
Adds path parameters to this path + method

:param dict api: Reference to the related Api's properties as defined in the template.
:param string path: Path name
:param string method_name: Method name
:param list path_parameters: list of strings of path parameters
"""
normalized_method_name = self._normalize_method_name(method_name)
for method_definition in self.get_method_contents(self.get_path(path)[normalized_method_name]):
# create path parameter list
# add it here if it doesn't exist, merge with existing otherwise.
method_definition.setdefault("parameters", [])
for param in path_parameters:
# find an existing parameter with this name if it exists
existing_parameter = next(
(
existing_parameter
for existing_parameter in method_definition.get("parameters", [])
if existing_parameter.get("name") == param
),
None,
)
if existing_parameter:
# overwrite parameter values for existing path parameter
existing_parameter["in"] = "path"
existing_parameter["required"] = True
else:
parameter = {"name": param, "in": "path", "required": True}
method_definition.get("parameters").append(parameter)

def add_authorizers_security_definitions(self, authorizers):
"""
Add Authorizer definitions to the securityDefinitions part of Swagger.
Expand Down
13 changes: 13 additions & 0 deletions tests/translator/input/http_api_existing_openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Resources:
Properties:
ApiId: !Ref MyApi
TimeoutInMillis: !Ref Timeout
PathParametersExisting:
Type: HttpApi
Properties:
ApiId: !Ref MyApi
Path: /get/{something}/with/{params}
Method: GET
MyApi:
Type: AWS::Serverless::HttpApi
Properties:
Expand Down Expand Up @@ -65,6 +71,13 @@ Resources:
- OpenIdAuth:
- scope3
responses: {}
"/get/{something}/with/{params}":
get:
parameters:
-
name: something
in: path
responses: {}
"/integration":
post:
x-amazon-apigateway-integration:
Expand Down
5 changes: 5 additions & 0 deletions tests/translator/input/implicit_http_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ Resources:
Properties:
Path: /basic2
Method: post
PathParameters:
Type: HttpApi
Properties:
Path: /get/{something}/with/{params}
Method: POST
25 changes: 25 additions & 0 deletions tests/translator/output/aws-cn/http_api_existing_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@
}
},
"paths": {
"/get/{something}/with/{params}": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
},
"responses": {},
"parameters": [
{
"required": true,
"name": "something",
"in": "path"
},
{
"required": true,
"name": "params",
"in": "path"
}
]
}
},
"/basic": {
"post": {
"x-amazon-apigateway-integration": {
Expand Down
182 changes: 114 additions & 68 deletions tests/translator/output/aws-cn/implicit_http_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
"HttpApiFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"arn:aws-cn:iam::aws:policy/AmazonDynamoDBFullAccess"
],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
Expand All @@ -36,6 +32,10 @@
}
]
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"arn:aws-cn:iam::aws:policy/AmazonDynamoDBFullAccess"
],
"Tags": [
{
"Value": "SAM",
Expand All @@ -47,10 +47,6 @@
"HttpApiFunction2Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"arn:aws-cn:iam::aws:policy/AmazonDynamoDBFullAccess"
],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
Expand All @@ -67,6 +63,77 @@
}
]
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"arn:aws-cn:iam::aws:policy/AmazonDynamoDBFullAccess"
],
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"HttpApiFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.restapi",
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "todo_list.zip"
},
"Role": {
"Fn::GetAtt": [
"HttpApiFunctionRole",
"Arn"
]
},
"Runtime": "nodejs12.x",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"HttpApiFunctionSimpleCasePermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"Principal": "apigateway.amazonaws.com",
"FunctionName": {
"Ref": "HttpApiFunction"
},
"SourceArn": {
"Fn::Sub": [
"arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/*",
{
"__Stage__": "*",
"__ApiId__": {
"Ref": "ServerlessHttpApi"
}
}
]
}
}
},
"HttpApiFunction2": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.restapi",
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "todo_list.zip"
},
"Role": {
"Fn::GetAtt": [
"HttpApiFunction2Role",
"Arn"
]
},
"Runtime": "nodejs12.x",
"Tags": [
{
"Value": "SAM",
Expand All @@ -85,12 +152,6 @@
"Ref": "AWS::StackName"
}
},
"tags": [
{
"name": "httpapi:createdBy",
"x-amazon-apigateway-tag-value": "SAM"
}
],
"paths": {
"/basic2": {
"post": {
Expand All @@ -105,17 +166,29 @@
"responses": {}
}
},
"/basic": {
"/get/{something}/with/{params}": {
"post": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction2.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
},
"responses": {}
"responses": {},
"parameters": [
{
"required": true,
"name": "something",
"in": "path"
},
{
"required": true,
"name": "params",
"in": "path"
}
]
}
},
"$default": {
Expand All @@ -131,23 +204,42 @@
"isDefaultRoute": true,
"responses": {}
}
},
"/basic": {
"post": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
},
"responses": {}
}
}
},
"openapi": "3.0.1"
"openapi": "3.0.1",
"tags": [
{
"name": "httpapi:createdBy",
"x-amazon-apigateway-tag-value": "SAM"
}
]
}
}
},
"HttpApiFunctionSimpleCasePermission": {
"HttpApiFunction2PathParametersPermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"Principal": "apigateway.amazonaws.com",
"FunctionName": {
"Ref": "HttpApiFunction"
"Ref": "HttpApiFunction2"
},
"SourceArn": {
"Fn::Sub": [
"arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/*",
"arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/POST/get/*/with/*",
{
"__Stage__": "*",
"__ApiId__": {
Expand All @@ -158,52 +250,6 @@
}
}
},
"HttpApiFunction2": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.restapi",
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "todo_list.zip"
},
"Role": {
"Fn::GetAtt": [
"HttpApiFunction2Role",
"Arn"
]
},
"Runtime": "nodejs12.x",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"HttpApiFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.restapi",
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "todo_list.zip"
},
"Role": {
"Fn::GetAtt": [
"HttpApiFunctionRole",
"Arn"
]
},
"Runtime": "nodejs12.x",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"HttpApiFunction2Basic2Permission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
Expand All @@ -226,4 +272,4 @@
}
}
}
}
}
Loading