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 warning when using default stage name and FailOnWarnings is on #2726

Merged
merged 8 commits into from
Dec 14, 2022
33 changes: 33 additions & 0 deletions integration/combination/test_http_api_with_fail_on_warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from unittest.case import skipIf
from parameterized import parameterized

from integration.helpers.base_test import BaseTest
from integration.helpers.resource import current_region_does_not_support
from integration.config.service_names import HTTP_API


@skipIf(current_region_does_not_support([HTTP_API]), "HttpApi is not supported in this testing region")
class TestHttpApiWithFailOnWarnings(BaseTest):
@parameterized.expand(
[
("combination/http_api_with_fail_on_warnings_and_default_stage_name", True),
("combination/http_api_with_fail_on_warnings_and_default_stage_name", False),
]
)
def test_http_api_with_fail_on_warnings(self, file_name, disable_value):
parameters = [
{
"ParameterKey": "FailOnWarningsValue",
"ParameterValue": "true" if disable_value else "false",
"UsePreviousValue": False,
"ResolvedValue": "string",
}
]

self.create_and_verify_stack(file_name, parameters)

http_api_id = self.get_physical_id_by_type("AWS::ApiGatewayV2::Api")
apigw_v2_client = self.client_provider.api_v2_client

api_result = apigw_v2_client.get_api(ApiId=http_api_id)
self.assertEqual(api_result["ResponseMetadata"]["HTTPStatusCode"], 200)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"LogicalResourceId": "AppFunctionAppHandlerPermission",
"ResourceType": "AWS::Lambda::Permission"
},
{
"LogicalResourceId": "AppApi",
"ResourceType": "AWS::ApiGatewayV2::Api"
},
{
"LogicalResourceId": "AppApiApiGatewayDefaultStage",
"ResourceType": "AWS::ApiGatewayV2::Stage"
},
{
"LogicalResourceId": "AppFunction",
"ResourceType": "AWS::Lambda::Function"
},
{
"LogicalResourceId": "AppFunctionRole",
"ResourceType": "AWS::IAM::Role"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Parameters:
FailOnWarningsValue:
Type: String
AllowedValues: [true, false]

Resources:
AppApi:
Type: AWS::Serverless::HttpApi
Properties:
FailOnWarnings: !Ref FailOnWarningsValue
StageName: $default
AppFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
InlineCode: |
def handler(event, context):
print("Hello, world!")
Runtime: python3.8
Architectures:
- x86_64
Events:
AppHandler:
Type: HttpApi
Properties:
ApiId: !Ref AppApi

Metadata:
SamTransformTest: true
14 changes: 14 additions & 0 deletions samtranslator/model/api/http_api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def _construct_http_api(self) -> ApiGatewayV2HttpApi:

self._add_title()
self._add_description()
self._update_default_path()

if self.definition_uri:
http_api.BodyS3Location = self._construct_body_s3_dict(self.definition_uri)
Expand Down Expand Up @@ -224,6 +225,19 @@ def _add_cors(self) -> None:
# Assign the OpenApi back to template
self.definition_body = editor.openapi

def _update_default_path(self) -> None:
# Only do the following if FailOnWarnings is enabled for backward compatibility.
if not self.fail_on_warnings or not self.definition_body:
return

# Using default stage name generate warning during deployment
# Warnings found during import: Parse issue: attribute paths.
# Resource $default should start with / (Service: AmazonApiGatewayV2; Status Code: 400;
# Deployment fails when FailOnWarnings is true: https://github.com/aws/serverless-application-model/issues/2297
paths: Dict[str, Any] = self.definition_body.get("paths", {})
if DefaultStageName in paths:
paths[f"/{DefaultStageName}"] = paths.pop(DefaultStageName)

def _construct_api_domain(
self, http_api: ApiGatewayV2HttpApi, route53_record_set_groups: Dict[str, Route53RecordSetGroup]
) -> Tuple[
Expand Down
2 changes: 1 addition & 1 deletion tests/translator/output/aws-cn/explicit_http_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
},
"openapi": "3.0.1",
"paths": {
"$default": {
"/$default": {
hoffa marked this conversation as resolved.
Show resolved Hide resolved
"x-amazon-apigateway-any-method": {
"isDefaultRoute": true,
"responses": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
},
"openapi": "3.0.1",
"paths": {
"$default": {
"/$default": {
"x-amazon-apigateway-any-method": {
"isDefaultRoute": true,
"responses": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"openapi": "3.0.1",
"paths": {
"$default": {
"/$default": {
"x-amazon-apigateway-any-method": {
"isDefaultRoute": true,
"responses": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
},
"openapi": "3.0.1",
"paths": {
"$default": {
"/$default": {
"x-amazon-apigateway-any-method": {
"isDefaultRoute": true,
"responses": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
},
"openapi": "3.0.1",
"paths": {
"$default": {
"/$default": {
"x-amazon-apigateway-any-method": {
"isDefaultRoute": true,
"responses": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"openapi": "3.0.1",
"paths": {
"$default": {
"/$default": {
"x-amazon-apigateway-any-method": {
"isDefaultRoute": true,
"responses": {},
Expand Down
2 changes: 1 addition & 1 deletion tests/translator/output/explicit_http_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
},
"openapi": "3.0.1",
"paths": {
"$default": {
"/$default": {
"x-amazon-apigateway-any-method": {
"isDefaultRoute": true,
"responses": {},
Expand Down
2 changes: 1 addition & 1 deletion tests/translator/output/http_api_explicit_stage.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
},
"openapi": "3.0.1",
"paths": {
"$default": {
"/$default": {
"x-amazon-apigateway-any-method": {
"isDefaultRoute": true,
"responses": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"openapi": "3.0.1",
"paths": {
"$default": {
"/$default": {
"x-amazon-apigateway-any-method": {
"isDefaultRoute": true,
"responses": {},
Expand Down