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: Don't attempt to refetch a swagger method object by its name as we already have it #2031

Merged
merged 3 commits into from
Jul 26, 2021
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
2 changes: 1 addition & 1 deletion samtranslator/open_api/open_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def set_path_default_authorizer(self, path, default_authorizer, authorizers, api
)
]
)
for method_definition in self.get_method_contents(self.get_path(path)[normalized_method_name]):
for method_definition in self.get_method_contents(method):
# If no integration given, then we don't need to process this definition (could be AWS::NoValue)
if not self.method_definition_has_integration(method_definition):
continue
Expand Down
7 changes: 3 additions & 4 deletions samtranslator/swagger/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ def set_path_default_authorizer(
if add_default_auth_to_preflight or normalized_method_name != "options":
normalized_method_name = self._normalize_method_name(method_name)
# It is possible that the method could have two definitions in a Fn::If block.
for method_definition in self.get_method_contents(self.get_path(path)[normalized_method_name]):
for method_definition in self.get_method_contents(method):

# If no integration given, then we don't need to process this definition (could be AWS::NoValue)
if not isinstance(method_definition, dict):
Expand Down Expand Up @@ -620,14 +620,13 @@ def set_path_default_apikey_required(self, path):
:param string path: Path name
"""

for method_name, _ in self.get_path(path).items():
for method_name, method in self.get_path(path).items():
# Excluding parameters section
if method_name == "parameters":
continue

normalized_method_name = self._normalize_method_name(method_name)
# It is possible that the method could have two definitions in a Fn::If block.
for method_definition in self.get_method_contents(self.get_path(path)[normalized_method_name]):
for method_definition in self.get_method_contents(method):

# If no integration given, then we don't need to process this definition (could be AWS::NoValue)
if not self.method_definition_has_integration(method_definition):
Expand Down
44 changes: 44 additions & 0 deletions tests/translator/input/api_with_any_method_in_swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Resources:
HttpApiFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/todo_list.zip
Handler: index.restapi
Runtime: python3.7
Events:
SimpleCase:
Type: HttpApi
Properties:
ApiId: !Ref MyApi
BasePath:
Type: HttpApi
Properties:
ApiId: !Ref MyApi
Path: /
Method: get

MyApi:
Type: AWS::Serverless::Api
Properties:
StageName:
Ref: Stage
Auth:
DefaultAuthorizer: "LambdaAuthorizer"
Authorizers:
LambdaAuthorizer:
FunctionPayloadType: "REQUEST"
Identity:
Headers:
- "Authorization"
DefinitionBody:
openapi: '3.0'
info:
title: !Sub ${AWS::StackName}-Api
paths:
/:
any:
x-amazon-apigateway-integration:
httpMethod: ANY
type: http_proxy
uri: https://www.alphavantage.co/
payloadFormatVersion: '1.0'
189 changes: 189 additions & 0 deletions tests/translator/output/api_with_any_method_in_swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
{
"Resources": {
"MyApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"info": {
"title": {
"Fn::Sub": "${AWS::StackName}-Api"
}
},
"paths": {
"$default": {
"x-amazon-apigateway-any-method": {
"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": "2.0"
},
"isDefaultRoute": true,
"security": [
{
"LambdaAuthorizer": []
}
],
"responses": {}
}
},
"/": {
"any": {
"x-amazon-apigateway-integration": {
"httpMethod": "ANY",
"type": "http_proxy",
"uri": "https://www.alphavantage.co/",
"payloadFormatVersion": "1.0"
},
"security": [
{
"LambdaAuthorizer": []
}
]
},
"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": "2.0"
},
"security": [
{
"LambdaAuthorizer": []
}
],
"responses": {}
}
}
},
"openapi": "3.0",
"components": {
"securitySchemes": {
"LambdaAuthorizer": {
"in": "header",
"type": "apiKey",
"name": "Unused",
"x-amazon-apigateway-authorizer": {
"type": "request",
"identitySource": "method.request.header.Authorization",
"authorizerUri": {
"Fn::Sub": [
"arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${__FunctionArn__}/invocations",
{
"__FunctionArn__": null
}
]
}
},
"x-amazon-apigateway-authtype": "custom"
}
}
}
}
}
},
"HttpApiFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.restapi",
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "todo_list.zip"
},
"Role": {
"Fn::GetAtt": [
"HttpApiFunctionRole",
"Arn"
]
},
"Runtime": "python3.7",
"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": "MyApi"
}
}
]
}
}
},
"HttpApiFunctionRole": {
"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": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"MyApiStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "MyApiDeployment69a80e7382"
},
"RestApiId": {
"Ref": "MyApi"
},
"StageName": {
"Ref": "Stage"
}
}
},
"MyApiDeployment69a80e7382": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "MyApi"
},
"Description": "RestApi deployment id: 69a80e738222706cff079ab8d7f348c0d89eddab",
"StageName": "Stage"
}
}
}
}
Loading