From a82a19f195e63f1a2bfc581d61dfa9e358a53486 Mon Sep 17 00:00:00 2001 From: Chris Rehn <1280602+hoffa@users.noreply.github.com> Date: Wed, 7 Dec 2022 11:56:48 -0800 Subject: [PATCH] fix: allow `Api` model without `properties` (#2709) --- samtranslator/swagger/swagger.py | 4 -- tests/swagger/test_swagger.py | 6 -- .../input/api_model_without_properties.yaml | 11 ++++ .../output/api_model_without_properties.json | 50 ++++++++++++++++ .../aws-cn/api_model_without_properties.json | 58 +++++++++++++++++++ .../api_model_without_properties.json | 58 +++++++++++++++++++ 6 files changed, 177 insertions(+), 10 deletions(-) create mode 100644 tests/translator/input/api_model_without_properties.yaml create mode 100644 tests/translator/output/api_model_without_properties.json create mode 100644 tests/translator/output/aws-cn/api_model_without_properties.json create mode 100644 tests/translator/output/aws-us-gov/api_model_without_properties.json diff --git a/samtranslator/swagger/swagger.py b/samtranslator/swagger/swagger.py index 2719129da..076d92f92 100644 --- a/samtranslator/swagger/swagger.py +++ b/samtranslator/swagger/swagger.py @@ -855,14 +855,10 @@ def add_models(self, models): # type: ignore[no-untyped-def] for model_name, schema in models.items(): model_type = schema.get("type") - model_properties = schema.get("properties") if not model_type: raise InvalidDocumentException([InvalidTemplateException("'Models' schema is missing 'type'.")]) - if not model_properties: - raise InvalidDocumentException([InvalidTemplateException("'Models' schema is missing 'properties'.")]) - self.definitions[model_name.lower()] = schema def add_resource_policy(self, resource_policy, path, stage): # type: ignore[no-untyped-def] diff --git a/tests/swagger/test_swagger.py b/tests/swagger/test_swagger.py index 84f0f762b..60964b562 100644 --- a/tests/swagger/test_swagger.py +++ b/tests/swagger/test_swagger.py @@ -692,12 +692,6 @@ def test_must_fail_without_type_in_model(self): with self.assertRaises(InvalidDocumentException): self.editor.add_models(models) - def test_must_fail_without_properties_in_model(self): - models = {"User": {"type": "object"}} - - with self.assertRaises(InvalidDocumentException): - self.editor.add_models(models) - class TestSwaggerEditor_add_request_model_to_method(TestCase): def setUp(self): diff --git a/tests/translator/input/api_model_without_properties.yaml b/tests/translator/input/api_model_without_properties.yaml new file mode 100644 index 000000000..94de0e2f7 --- /dev/null +++ b/tests/translator/input/api_model_without_properties.yaml @@ -0,0 +1,11 @@ +Resources: + MyApi: + Type: AWS::Serverless::Api + Properties: + StageName: Prod + Models: + MyModel: + items: + type: integer + type: array + title: MyModel diff --git a/tests/translator/output/api_model_without_properties.json b/tests/translator/output/api_model_without_properties.json new file mode 100644 index 000000000..c01135269 --- /dev/null +++ b/tests/translator/output/api_model_without_properties.json @@ -0,0 +1,50 @@ +{ + "Resources": { + "MyApi": { + "Properties": { + "Body": { + "definitions": { + "mymodel": { + "items": { + "type": "integer" + }, + "title": "MyModel", + "type": "array" + } + }, + "info": { + "title": { + "Ref": "AWS::StackName" + }, + "version": "1.0" + }, + "paths": {}, + "swagger": "2.0" + } + }, + "Type": "AWS::ApiGateway::RestApi" + }, + "MyApiDeployment49616beda5": { + "Properties": { + "Description": "RestApi deployment id: 49616beda518d56ac1dbce9c34f09ccca4862616", + "RestApiId": { + "Ref": "MyApi" + }, + "StageName": "Stage" + }, + "Type": "AWS::ApiGateway::Deployment" + }, + "MyApiProdStage": { + "Properties": { + "DeploymentId": { + "Ref": "MyApiDeployment49616beda5" + }, + "RestApiId": { + "Ref": "MyApi" + }, + "StageName": "Prod" + }, + "Type": "AWS::ApiGateway::Stage" + } + } +} diff --git a/tests/translator/output/aws-cn/api_model_without_properties.json b/tests/translator/output/aws-cn/api_model_without_properties.json new file mode 100644 index 000000000..5d5f54d8d --- /dev/null +++ b/tests/translator/output/aws-cn/api_model_without_properties.json @@ -0,0 +1,58 @@ +{ + "Resources": { + "MyApi": { + "Properties": { + "Body": { + "definitions": { + "mymodel": { + "items": { + "type": "integer" + }, + "title": "MyModel", + "type": "array" + } + }, + "info": { + "title": { + "Ref": "AWS::StackName" + }, + "version": "1.0" + }, + "paths": {}, + "swagger": "2.0" + }, + "EndpointConfiguration": { + "Types": [ + "REGIONAL" + ] + }, + "Parameters": { + "endpointConfigurationTypes": "REGIONAL" + } + }, + "Type": "AWS::ApiGateway::RestApi" + }, + "MyApiDeployment49616beda5": { + "Properties": { + "Description": "RestApi deployment id: 49616beda518d56ac1dbce9c34f09ccca4862616", + "RestApiId": { + "Ref": "MyApi" + }, + "StageName": "Stage" + }, + "Type": "AWS::ApiGateway::Deployment" + }, + "MyApiProdStage": { + "Properties": { + "DeploymentId": { + "Ref": "MyApiDeployment49616beda5" + }, + "RestApiId": { + "Ref": "MyApi" + }, + "StageName": "Prod" + }, + "Type": "AWS::ApiGateway::Stage" + } + } +} diff --git a/tests/translator/output/aws-us-gov/api_model_without_properties.json b/tests/translator/output/aws-us-gov/api_model_without_properties.json new file mode 100644 index 000000000..5d5f54d8d --- /dev/null +++ b/tests/translator/output/aws-us-gov/api_model_without_properties.json @@ -0,0 +1,58 @@ +{ + "Resources": { + "MyApi": { + "Properties": { + "Body": { + "definitions": { + "mymodel": { + "items": { + "type": "integer" + }, + "title": "MyModel", + "type": "array" + } + }, + "info": { + "title": { + "Ref": "AWS::StackName" + }, + "version": "1.0" + }, + "paths": {}, + "swagger": "2.0" + }, + "EndpointConfiguration": { + "Types": [ + "REGIONAL" + ] + }, + "Parameters": { + "endpointConfigurationTypes": "REGIONAL" + } + }, + "Type": "AWS::ApiGateway::RestApi" + }, + "MyApiDeployment49616beda5": { + "Properties": { + "Description": "RestApi deployment id: 49616beda518d56ac1dbce9c34f09ccca4862616", + "RestApiId": { + "Ref": "MyApi" + }, + "StageName": "Stage" + }, + "Type": "AWS::ApiGateway::Deployment" + }, + "MyApiProdStage": { + "Properties": { + "DeploymentId": { + "Ref": "MyApiDeployment49616beda5" + }, + "RestApiId": { + "Ref": "MyApi" + }, + "StageName": "Prod" + }, + "Type": "AWS::ApiGateway::Stage" + } + } +}