Skip to content

Commit 87d2a88

Browse files
authored
fix: fix paths IF intrinsic bug (#2330)
1 parent 2956d26 commit 87d2a88

File tree

42 files changed

+7692
-471
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+7692
-471
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,8 @@ def _get_permission(self, resources_to_link, stage):
11181118
# If this is using the new $default path, keep path blank and add a * permission
11191119
if path == OpenApiEditor._DEFAULT_PATH:
11201120
path = ""
1121-
elif editor and resources_to_link.get("function").logical_id == editor.get_integration_function_logical_id(
1122-
OpenApiEditor._DEFAULT_PATH, OpenApiEditor._X_ANY_METHOD
1121+
elif editor and editor.is_integration_function_logical_id_match(
1122+
OpenApiEditor._DEFAULT_PATH, OpenApiEditor._X_ANY_METHOD, resources_to_link.get("function").logical_id
11231123
):
11241124
# Case where default exists for this function, and so the permissions for that will apply here as well
11251125
# This can save us several CFN resources (not duplicating permissions)

samtranslator/open_api/open_api.py

Lines changed: 165 additions & 138 deletions
Large diffs are not rendered by default.

samtranslator/swagger/swagger.py

Lines changed: 279 additions & 303 deletions
Large diffs are not rendered by default.

tests/openapi/test_openapi.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def setUp(self):
367367
self.editor = OpenApiEditor(self.original_openapi)
368368

369369

370-
class TestOpenApiEditor_get_integration_function(TestCase):
370+
class TestOpenApiEditor_is_integration_function_logical_id_match(TestCase):
371371
def setUp(self):
372372

373373
self.original_openapi = {
@@ -407,13 +407,14 @@ def setUp(self):
407407

408408
self.editor = OpenApiEditor(self.original_openapi)
409409

410-
def test_must_get_integration_function_if_exists(self):
410+
def test_must_match_integration_function_if_exists(self):
411411

412-
self.assertEqual(
413-
self.editor.get_integration_function_logical_id(OpenApiEditor._DEFAULT_PATH, OpenApiEditor._X_ANY_METHOD),
414-
"HttpApiFunction",
412+
self.assertTrue(
413+
self.editor.is_integration_function_logical_id_match(
414+
OpenApiEditor._DEFAULT_PATH, OpenApiEditor._X_ANY_METHOD, "HttpApiFunction"
415+
),
415416
)
416-
self.assertFalse(self.editor.get_integration_function_logical_id("/bar", "get"))
417+
self.assertFalse(self.editor.is_integration_function_logical_id_match("/bar", "get", "HttpApiFunction"))
417418

418419

419420
class TestOpenApiEdit_add_description(TestCase):
@@ -439,7 +440,7 @@ def test_must_not_add_description_if_already_defined(self):
439440
self.assertEqual(editor.openapi["info"]["description"], "Existing Description")
440441

441442

442-
class TestOpenApiEditor_get_integration_function_of_alias(TestCase):
443+
class TestOpenApiEditor_is_integration_function_logical_id_match_with_alias(TestCase):
443444
def setUp(self):
444445

445446
self.original_openapi = {
@@ -479,8 +480,10 @@ def setUp(self):
479480

480481
self.editor = OpenApiEditor(self.original_openapi)
481482

482-
def test_no_logical_id_if_alias(self):
483+
def test_no_match_if_alias(self):
483484

484485
self.assertFalse(
485-
self.editor.get_integration_function_logical_id(OpenApiEditor._DEFAULT_PATH, OpenApiEditor._X_ANY_METHOD),
486+
self.editor.is_integration_function_logical_id_match(
487+
OpenApiEditor._DEFAULT_PATH, OpenApiEditor._X_ANY_METHOD, "HttpApiFunctionAlias"
488+
),
486489
)

tests/swagger/test_swagger.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ def test_must_make_default_value_with_optional_allowed_methods(self):
385385
expected = {"some cors": "return value"}
386386
path = "/foo"
387387

388-
self.editor._make_cors_allowed_methods_for_path = Mock()
389-
self.editor._make_cors_allowed_methods_for_path.return_value = default_allow_methods_value
388+
self.editor._make_cors_allowed_methods_for_path_item = Mock()
389+
self.editor._make_cors_allowed_methods_for_path_item.return_value = default_allow_methods_value
390390

391391
self.editor._options_method_response_for_cors = Mock()
392392
self.editor._options_method_response_for_cors.return_value = expected
@@ -578,45 +578,39 @@ def test_allow_credentials_is_skipped_with_false_value(self):
578578
self.assertEqual(expected, actual)
579579

580580

581-
class TestSwaggerEditor_make_cors_allowed_methods_for_path(TestCase):
581+
class TestSwaggerEditor_make_cors_allowed_methods_for_path_item(TestCase):
582582
def setUp(self):
583+
self.foo_path_item = {"get": {}, "POST": {}, "DeLeTe": {}}
584+
self.withany_path_item = {"head": {}, _X_ANY_METHOD: {}}
585+
self.nothing_path_item = {}
586+
583587
self.editor = SwaggerEditor(
584588
{
585589
"swagger": "2.0",
586590
"paths": {
587-
"/foo": {"get": {}, "POST": {}, "DeLeTe": {}},
588-
"/withany": {"head": {}, _X_ANY_METHOD: {}},
589-
"/nothing": {},
591+
"/foo": self.foo_path_item,
592+
"/withany": self.withany_path_item,
593+
"/nothing": self.nothing_path_item,
590594
},
591595
}
592596
)
593597

594598
def test_must_return_all_defined_methods(self):
595-
path = "/foo"
596599
expected = "DELETE,GET,OPTIONS,POST" # Result should be sorted alphabetically
597600

598-
actual = self.editor._make_cors_allowed_methods_for_path(path)
601+
actual = self.editor._make_cors_allowed_methods_for_path_item(self.foo_path_item)
599602
self.assertEqual(expected, actual)
600603

601604
def test_must_work_for_any_method(self):
602-
path = "/withany"
603605
expected = "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT" # Result should be sorted alphabetically
604606

605-
actual = self.editor._make_cors_allowed_methods_for_path(path)
607+
actual = self.editor._make_cors_allowed_methods_for_path_item(self.withany_path_item)
606608
self.assertEqual(expected, actual)
607609

608610
def test_must_work_with_no_methods(self):
609-
path = "/nothing"
610611
expected = "OPTIONS"
611612

612-
actual = self.editor._make_cors_allowed_methods_for_path(path)
613-
self.assertEqual(expected, actual)
614-
615-
def test_must_skip_non_existent_path(self):
616-
path = "/no-path"
617-
expected = ""
618-
619-
actual = self.editor._make_cors_allowed_methods_for_path(path)
613+
actual = self.editor._make_cors_allowed_methods_for_path_item(self.nothing_path_item)
620614
self.assertEqual(expected, actual)
621615

622616

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
sam-app
5+
6+
Sample SAM Template for sam-app
7+
8+
Conditions:
9+
TrueCondition:
10+
Fn::Equals:
11+
- true
12+
- true
13+
FalseCondition:
14+
Fn::Equals:
15+
- true
16+
- false
17+
18+
Resources:
19+
MyAuthFn:
20+
Type: AWS::Serverless::Function
21+
Properties:
22+
CodeUri: s3://sam-demo-bucket/hello.zip
23+
Handler: index.handler
24+
Runtime: nodejs12.x
25+
26+
MyApi:
27+
Type: AWS::Serverless::HttpApi
28+
Properties:
29+
Auth:
30+
Authorizers:
31+
MyLambdaAuthUpdated:
32+
FunctionArn:
33+
Fn::GetAtt:
34+
- MyAuthFn
35+
- Arn
36+
FunctionInvokeRole:
37+
Fn::GetAtt:
38+
- MyAuthFnRole
39+
- Arn
40+
Identity:
41+
Headers:
42+
- Authorization
43+
ReauthorizeEvery: 37
44+
EnableSimpleResponses: false
45+
AuthorizerPayloadFormatVersion: 1.0
46+
DefaultAuthorizer: MyLambdaAuthUpdated
47+
DefinitionBody:
48+
openapi: '3.0'
49+
info:
50+
title: !Sub ${AWS::StackName}-Api
51+
paths:
52+
/post:
53+
Fn::If:
54+
- FalseCondition
55+
-
56+
post:
57+
x-amazon-apigateway-integration:
58+
httpMethod: POST
59+
type: aws_proxy
60+
uri: https://www.otherURI.co/
61+
payloadFormatVersion: '1.0'
62+
-
63+
post:
64+
x-amazon-apigateway-integration:
65+
httpMethod: POST
66+
type: aws_proxy
67+
uri: https://www.alphavantage.co/
68+
payloadFormatVersion: '1.0'
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
sam-app
5+
6+
Sample SAM Template for sam-app
7+
8+
Conditions:
9+
TrueCondition:
10+
Fn::Equals:
11+
- true
12+
- true
13+
FalseCondition:
14+
Fn::Equals:
15+
- true
16+
- false
17+
18+
Resources:
19+
MyAuthFn:
20+
Type: AWS::Serverless::Function
21+
Properties:
22+
CodeUri: s3://sam-demo-bucket/hello.zip
23+
Handler: index.handler
24+
Runtime: nodejs12.x
25+
26+
MyApi:
27+
Type: AWS::Serverless::HttpApi
28+
Properties:
29+
Auth:
30+
Authorizers:
31+
MyLambdaAuthUpdated:
32+
FunctionArn:
33+
Fn::GetAtt:
34+
- MyAuthFn
35+
- Arn
36+
FunctionInvokeRole:
37+
Fn::GetAtt:
38+
- MyAuthFnRole
39+
- Arn
40+
Identity:
41+
Headers:
42+
- Authorization
43+
ReauthorizeEvery: 37
44+
EnableSimpleResponses: false
45+
AuthorizerPayloadFormatVersion: 1.0
46+
DefaultAuthorizer: MyLambdaAuthUpdated
47+
DefinitionBody:
48+
openapi: '3.0'
49+
info:
50+
title: !Sub ${AWS::StackName}-Api
51+
paths:
52+
/post:
53+
Fn::If:
54+
- FalseCondition
55+
-
56+
post:
57+
x-amazon-apigateway-integration:
58+
httpMethod: POST
59+
type: aws_proxy
60+
uri: https://www.alphavantage.co/
61+
payloadFormatVersion: '1.0'
62+
- Ref: "AWS::NoValue"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
sam-app
5+
6+
Sample SAM Template for sam-app
7+
8+
Conditions:
9+
TrueCondition:
10+
Fn::Equals:
11+
- true
12+
- true
13+
FalseCondition:
14+
Fn::Equals:
15+
- true
16+
- false
17+
18+
Resources:
19+
MyAuthFn:
20+
Type: AWS::Serverless::Function
21+
Properties:
22+
CodeUri: s3://sam-demo-bucket/hello.zip
23+
Handler: index.handler
24+
Runtime: nodejs12.x
25+
26+
MyApi:
27+
Type: AWS::Serverless::HttpApi
28+
Properties:
29+
Auth:
30+
Authorizers:
31+
MyLambdaAuthUpdated:
32+
FunctionArn:
33+
Fn::GetAtt:
34+
- MyAuthFn
35+
- Arn
36+
FunctionInvokeRole:
37+
Fn::GetAtt:
38+
- MyAuthFnRole
39+
- Arn
40+
Identity:
41+
Headers:
42+
- Authorization
43+
ReauthorizeEvery: 37
44+
EnableSimpleResponses: false
45+
AuthorizerPayloadFormatVersion: 1.0
46+
DefaultAuthorizer: MyLambdaAuthUpdated
47+
DefinitionBody:
48+
openapi: '3.0'
49+
info:
50+
title: !Sub ${AWS::StackName}-Api
51+
paths:
52+
/post:
53+
Fn::If:
54+
- FalseCondition
55+
- Ref: "AWS::NoValue"
56+
-
57+
post:
58+
x-amazon-apigateway-integration:
59+
httpMethod: POST
60+
type: aws_proxy
61+
uri: https://www.alphavantage.co/
62+
payloadFormatVersion: '1.0'

0 commit comments

Comments
 (0)