From 53ac375809b968f9332e21c24daf6f6c9c2fdad7 Mon Sep 17 00:00:00 2001 From: Mohamed Elasmar Date: Mon, 8 Nov 2021 14:14:40 -0800 Subject: [PATCH 1/4] raise a validation exception if the responses section in options method for a path in OpenApi Definition --- samtranslator/model/api/api_generator.py | 11 ++++++++++- .../input/error_api_invalid_openapi_path.yaml | 13 +++++++++++++ ...invalid_openapi_path_with_empty_responses.yaml | 15 +++++++++++++++ .../output/error_api_invalid_openapi_path.json | 3 +++ ...invalid_openapi_path_with_empty_responses.json | 3 +++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/translator/input/error_api_invalid_openapi_path.yaml create mode 100644 tests/translator/input/error_api_invalid_openapi_path_with_empty_responses.yaml create mode 100644 tests/translator/output/error_api_invalid_openapi_path.json create mode 100644 tests/translator/output/error_api_invalid_openapi_path_with_empty_responses.json diff --git a/samtranslator/model/api/api_generator.py b/samtranslator/model/api/api_generator.py index fba368f8a4..8f749c5afb 100644 --- a/samtranslator/model/api/api_generator.py +++ b/samtranslator/model/api/api_generator.py @@ -18,7 +18,7 @@ ApiGatewayApiKey, ) from samtranslator.model.route53 import Route53RecordSetGroup -from samtranslator.model.exceptions import InvalidResourceException, InvalidTemplateException +from samtranslator.model.exceptions import InvalidResourceException, InvalidTemplateException, InvalidDocumentException from samtranslator.model.s3_utils.uri_parser import parse_s3_uri from samtranslator.region_configuration import RegionConfiguration from samtranslator.swagger.swagger import SwaggerEditor @@ -980,6 +980,15 @@ def _openapi_postprocess(self, definition_body): # add schema for the headers in options section for openapi3 if field in ["responses"]: options_path = definition_body["paths"][path]["options"] + if options_path and options_path.get(field) is None: + raise InvalidDocumentException( + [ + InvalidTemplateException( + "Value of responses in options method for path {} must be a dictionary." + .format(path) + ) + ] + ) if ( options_path and options_path.get(field).get("200") diff --git a/tests/translator/input/error_api_invalid_openapi_path.yaml b/tests/translator/input/error_api_invalid_openapi_path.yaml new file mode 100644 index 0000000000..db21fc2c20 --- /dev/null +++ b/tests/translator/input/error_api_invalid_openapi_path.yaml @@ -0,0 +1,13 @@ +Resources: + ApiWithInvalidPath: + Type: AWS::Serverless::Api + Properties: + StageName: Prod + Cors: "'*'" + OpenApiVersion: 3.0.1 + DefinitionBody: + openapi: 3.0.1 + info: + title: test invalid paths Api + paths: + /foo: \ No newline at end of file diff --git a/tests/translator/input/error_api_invalid_openapi_path_with_empty_responses.yaml b/tests/translator/input/error_api_invalid_openapi_path_with_empty_responses.yaml new file mode 100644 index 0000000000..b2a2e52368 --- /dev/null +++ b/tests/translator/input/error_api_invalid_openapi_path_with_empty_responses.yaml @@ -0,0 +1,15 @@ +Resources: + ApiWithInvalidPath: + Type: AWS::Serverless::Api + Properties: + StageName: Prod + Cors: "'*'" + OpenApiVersion: 3.0.1 + DefinitionBody: + openapi: 3.0.1 + info: + title: test invalid paths Api + paths: + /foo: + options: + responses: \ No newline at end of file diff --git a/tests/translator/output/error_api_invalid_openapi_path.json b/tests/translator/output/error_api_invalid_openapi_path.json new file mode 100644 index 0000000000..2aedc06844 --- /dev/null +++ b/tests/translator/output/error_api_invalid_openapi_path.json @@ -0,0 +1,3 @@ +{ + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. Value of '/foo' path must be a dictionary according to Swagger spec." +} \ No newline at end of file diff --git a/tests/translator/output/error_api_invalid_openapi_path_with_empty_responses.json b/tests/translator/output/error_api_invalid_openapi_path_with_empty_responses.json new file mode 100644 index 0000000000..d256f67567 --- /dev/null +++ b/tests/translator/output/error_api_invalid_openapi_path_with_empty_responses.json @@ -0,0 +1,3 @@ +{ + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. Value of responses in options method for path /foo must be a dictionary." +} \ No newline at end of file From ccd3c21d4d224f3e1eca1416aca58da2bbb7b911 Mon Sep 17 00:00:00 2001 From: Mohamed Elasmar Date: Mon, 8 Nov 2021 14:44:32 -0800 Subject: [PATCH 2/4] black formating --- samtranslator/model/api/api_generator.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samtranslator/model/api/api_generator.py b/samtranslator/model/api/api_generator.py index 8f749c5afb..2391370d49 100644 --- a/samtranslator/model/api/api_generator.py +++ b/samtranslator/model/api/api_generator.py @@ -984,8 +984,9 @@ def _openapi_postprocess(self, definition_body): raise InvalidDocumentException( [ InvalidTemplateException( - "Value of responses in options method for path {} must be a dictionary." - .format(path) + "Value of responses in options method for path {} must be a dictionary.".format( + path + ) ) ] ) From 3a108ea0f7e5b04d49a9157979dd0c72cbbde482 Mon Sep 17 00:00:00 2001 From: Mohamed Elasmar Date: Mon, 8 Nov 2021 15:11:54 -0800 Subject: [PATCH 3/4] validate the responses section should be a dictionary not only not null. --- samtranslator/model/api/api_generator.py | 7 +++---- ...nvalid_openapi_path_with_string_responses.yaml | 15 +++++++++++++++ ...invalid_openapi_path_with_empty_responses.json | 2 +- ...nvalid_openapi_path_with_string_responses.json | 3 +++ 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 tests/translator/input/error_api_invalid_openapi_path_with_string_responses.yaml create mode 100644 tests/translator/output/error_api_invalid_openapi_path_with_string_responses.json diff --git a/samtranslator/model/api/api_generator.py b/samtranslator/model/api/api_generator.py index 2391370d49..7ab6c61de1 100644 --- a/samtranslator/model/api/api_generator.py +++ b/samtranslator/model/api/api_generator.py @@ -980,13 +980,12 @@ def _openapi_postprocess(self, definition_body): # add schema for the headers in options section for openapi3 if field in ["responses"]: options_path = definition_body["paths"][path]["options"] - if options_path and options_path.get(field) is None: + if options_path and not isinstance(options_path.get(field), dict): raise InvalidDocumentException( [ InvalidTemplateException( - "Value of responses in options method for path {} must be a dictionary.".format( - path - ) + f"Value of responses in options method for path {path} must be a " + f"dictionary according to Swagger spec." ) ] ) diff --git a/tests/translator/input/error_api_invalid_openapi_path_with_string_responses.yaml b/tests/translator/input/error_api_invalid_openapi_path_with_string_responses.yaml new file mode 100644 index 0000000000..596839e458 --- /dev/null +++ b/tests/translator/input/error_api_invalid_openapi_path_with_string_responses.yaml @@ -0,0 +1,15 @@ +Resources: + ApiWithInvalidPath: + Type: AWS::Serverless::Api + Properties: + StageName: Prod + Cors: "'*'" + OpenApiVersion: 3.0.1 + DefinitionBody: + openapi: 3.0.1 + info: + title: test invalid paths Api + paths: + /foo: + options: + responses: invalid \ No newline at end of file diff --git a/tests/translator/output/error_api_invalid_openapi_path_with_empty_responses.json b/tests/translator/output/error_api_invalid_openapi_path_with_empty_responses.json index d256f67567..73a7f49ac3 100644 --- a/tests/translator/output/error_api_invalid_openapi_path_with_empty_responses.json +++ b/tests/translator/output/error_api_invalid_openapi_path_with_empty_responses.json @@ -1,3 +1,3 @@ { - "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. Value of responses in options method for path /foo must be a dictionary." + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. Value of responses in options method for path /foo must be a dictionary according to Swagger spec." } \ No newline at end of file diff --git a/tests/translator/output/error_api_invalid_openapi_path_with_string_responses.json b/tests/translator/output/error_api_invalid_openapi_path_with_string_responses.json new file mode 100644 index 0000000000..73a7f49ac3 --- /dev/null +++ b/tests/translator/output/error_api_invalid_openapi_path_with_string_responses.json @@ -0,0 +1,3 @@ +{ + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. Value of responses in options method for path /foo must be a dictionary according to Swagger spec." +} \ No newline at end of file From 37ca8f458b799b6c40ed381e09f057d5a0524692 Mon Sep 17 00:00:00 2001 From: Mohamed Elasmar Date: Mon, 8 Nov 2021 15:21:35 -0800 Subject: [PATCH 4/4] fix py27 issue --- samtranslator/model/api/api_generator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samtranslator/model/api/api_generator.py b/samtranslator/model/api/api_generator.py index 7ab6c61de1..04cf78e749 100644 --- a/samtranslator/model/api/api_generator.py +++ b/samtranslator/model/api/api_generator.py @@ -984,8 +984,8 @@ def _openapi_postprocess(self, definition_body): raise InvalidDocumentException( [ InvalidTemplateException( - f"Value of responses in options method for path {path} must be a " - f"dictionary according to Swagger spec." + "Value of responses in options method for path {} must be a " + "dictionary according to Swagger spec.".format(path) ) ] )