diff --git a/samtranslator/__init__.py b/samtranslator/__init__.py index 84c54b7482..82b6a36f8b 100644 --- a/samtranslator/__init__.py +++ b/samtranslator/__init__.py @@ -1 +1 @@ -__version__ = '1.13.0' +__version__ = '1.13.1' diff --git a/samtranslator/model/sam_resources.py b/samtranslator/model/sam_resources.py index 5f251ecefe..64890b715a 100644 --- a/samtranslator/model/sam_resources.py +++ b/samtranslator/model/sam_resources.py @@ -474,6 +474,9 @@ def to_cloudformation(self, **kwargs): """ resources = [] + intrinsics_resolver = kwargs["intrinsics_resolver"] + self.BinaryMediaTypes = intrinsics_resolver.resolve_parameter_refs(self.BinaryMediaTypes) + api_generator = ApiGenerator(self.logical_id, self.CacheClusterEnabled, self.CacheClusterSize, diff --git a/samtranslator/swagger/swagger.py b/samtranslator/swagger/swagger.py index c823e2a6a0..a1fecb3444 100644 --- a/samtranslator/swagger/swagger.py +++ b/samtranslator/swagger/swagger.py @@ -1,4 +1,5 @@ import copy +import json import re from six import string_types @@ -257,7 +258,8 @@ def add_cors(self, path, allowed_origins, allowed_headers=None, allowed_methods= allow_credentials) def add_binary_media_types(self, binary_media_types): - self._doc[self._X_APIGW_BINARY_MEDIA_TYPES] = binary_media_types + bmt = json.loads(json.dumps(binary_media_types).replace('~1', '/')) + self._doc[self._X_APIGW_BINARY_MEDIA_TYPES] = bmt def _options_method_response_for_cors(self, allowed_origins, allowed_headers=None, allowed_methods=None, max_age=None, allow_credentials=None): diff --git a/tests/translator/input/api_with_binary_media_types.yaml b/tests/translator/input/api_with_binary_media_types.yaml index 0aa8f85916..dfe4ed0f22 100644 --- a/tests/translator/input/api_with_binary_media_types.yaml +++ b/tests/translator/input/api_with_binary_media_types.yaml @@ -1,8 +1,12 @@ +Parameters: + BMT: + Type: String + Default: 'image~1jpg' Globals: Api: BinaryMediaTypes: - - image/jpg - - {"Fn::Join": ["/", ["image", "png"]]} + - image~1gif + - {"Fn::Join": ["~1", ["image", "png"]]} Resources: ImplicitApiFunction: @@ -24,4 +28,5 @@ Resources: StageName: Prod DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json BinaryMediaTypes: - - image/gif + - application~1octet-stream + - !Ref BMT diff --git a/tests/translator/input/api_with_binary_media_types_definition_body.yaml b/tests/translator/input/api_with_binary_media_types_definition_body.yaml index ecbbb01220..6730419d4f 100644 --- a/tests/translator/input/api_with_binary_media_types_definition_body.yaml +++ b/tests/translator/input/api_with_binary_media_types_definition_body.yaml @@ -1,8 +1,13 @@ +Parameters: + BMT: + Type: String + Default: image~1jpeg Globals: Api: BinaryMediaTypes: - - image/jpg - - {"Fn::Join": ["/", ["image", "png"]]} + - !Ref BMT + - image~1jpg + - {"Fn::Join": ["~1", ["image", "png"]]} Resources: ExplicitApiManagedSwagger: @@ -10,14 +15,14 @@ Resources: Properties: StageName: Prod BinaryMediaTypes: - - image/gif + - image~1gif ExplicitApiDefinitionBody: Type: AWS::Serverless::Api Properties: StageName: Prod BinaryMediaTypes: - - application/json + - application~1json DefinitionBody: { "paths": {}, "swagger": "2.0", diff --git a/tests/translator/output/api_with_binary_media_types.json b/tests/translator/output/api_with_binary_media_types.json index 75a3bd69f7..e157d5f5f1 100644 --- a/tests/translator/output/api_with_binary_media_types.json +++ b/tests/translator/output/api_with_binary_media_types.json @@ -1,4 +1,10 @@ { + "Parameters": { + "BMT": { + "Default": "image~1jpg", + "Type": "String" + } + }, "Resources": { "ImplicitApiFunction": { "Type": "AWS::Lambda::Function", @@ -47,16 +53,6 @@ } } }, - "ExplicitApiDeploymentf117c932f7": { - "Type": "AWS::ApiGateway::Deployment", - "Properties": { - "RestApiId": { - "Ref": "ExplicitApi" - }, - "Description": "RestApi deployment id: f117c932f75cfa87d23dfed64e9430d0081ef289", - "StageName": "Stage" - } - }, "ImplicitApiFunctionGetHtmlPermissionTest": { "Type": "AWS::Lambda::Permission", "Properties": { @@ -103,17 +99,18 @@ "Type": "AWS::ApiGateway::RestApi", "Properties": { "BinaryMediaTypes": [ - "image/jpg", + "image~1gif", { "Fn::Join": [ - "/", + "~1", [ "image", "png" ] ] }, - "image/gif" + "application~1octet-stream", + "image~1jpg" ], "BodyS3Location": { "Bucket": "sam-demo-bucket", @@ -137,7 +134,7 @@ "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ServerlessRestApiDeployment706c6ba929" + "Ref": "ServerlessRestApiDeployment4f3fcf6bd1" }, "RestApiId": { "Ref": "ServerlessRestApi" @@ -145,13 +142,23 @@ "StageName": "Prod" } }, - "ServerlessRestApiDeployment706c6ba929": { + "ExplicitApiDeploymentf117c932f7": { + "Type": "AWS::ApiGateway::Deployment", + "Properties": { + "RestApiId": { + "Ref": "ExplicitApi" + }, + "Description": "RestApi deployment id: f117c932f75cfa87d23dfed64e9430d0081ef289", + "StageName": "Stage" + } + }, + "ServerlessRestApiDeployment4f3fcf6bd1": { "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { "Ref": "ServerlessRestApi" }, - "Description": "RestApi deployment id: 706c6ba9298c730536b2f4c51409cf33e5d616d4", + "Description": "RestApi deployment id: 4f3fcf6bd13686961885c787e3a117b084ec6c3a", "StageName": "Stage" } }, @@ -181,7 +188,7 @@ }, "swagger": "2.0", "x-amazon-apigateway-binary-media-types": [ - "image/jpg", + "image/gif", { "Fn::Join": [ "/", @@ -194,10 +201,10 @@ ] }, "BinaryMediaTypes": [ - "image/jpg", + "image~1gif", { "Fn::Join": [ - "/", + "~1", [ "image", "png" @@ -208,4 +215,4 @@ } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/api_with_binary_media_types_definition_body.json b/tests/translator/output/api_with_binary_media_types_definition_body.json index be8dce728b..6a9e500f8b 100644 --- a/tests/translator/output/api_with_binary_media_types_definition_body.json +++ b/tests/translator/output/api_with_binary_media_types_definition_body.json @@ -1,20 +1,16 @@ { + "Parameters": { + "BMT": { + "Default": "image~1jpeg", + "Type": "String" + } + }, "Resources": { - "ExplicitApiManagedSwaggerDeployment51ba79c0e7": { - "Type": "AWS::ApiGateway::Deployment", - "Properties": { - "RestApiId": { - "Ref": "ExplicitApiManagedSwagger" - }, - "Description": "RestApi deployment id: 51ba79c0e7185c019037fb7f9f9f2235547af57b", - "StageName": "Stage" - } - }, "ExplicitApiManagedSwaggerProdStage": { "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ExplicitApiManagedSwaggerDeployment51ba79c0e7" + "Ref": "ExplicitApiManagedSwaggerDeploymentfe9c2c09a2" }, "RestApiId": { "Ref": "ExplicitApiManagedSwagger" @@ -22,13 +18,13 @@ "StageName": "Prod" } }, - "ExplicitApiDefinitionBodyDeploymentcb68c305fb": { + "ExplicitApiManagedSwaggerDeploymentfe9c2c09a2": { "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { - "Ref": "ExplicitApiDefinitionBody" + "Ref": "ExplicitApiManagedSwagger" }, - "Description": "RestApi deployment id: cb68c305fb80d8e723c6c5d51a7b62462a25f5ba", + "Description": "RestApi deployment id: fe9c2c09a27ce00c2fa53d5a491cf343e6a3278e", "StageName": "Stage" } }, @@ -39,6 +35,7 @@ "paths": {}, "swagger": "2.0", "x-amazon-apigateway-binary-media-types": [ + "image/jpeg", "image/jpg", { "Fn::Join": [ @@ -53,25 +50,36 @@ ] }, "BinaryMediaTypes": [ - "image/jpg", + "image~1jpeg", + "image~1jpg", { "Fn::Join": [ - "/", + "~1", [ "image", "png" ] ] }, - "application/json" + "application~1json" ] } }, + "ExplicitApiDefinitionBodyDeployment1f26996adb": { + "Type": "AWS::ApiGateway::Deployment", + "Properties": { + "RestApiId": { + "Ref": "ExplicitApiDefinitionBody" + }, + "Description": "RestApi deployment id: 1f26996adbe5077359ecb2bb0688a9cc0ae8e4fc", + "StageName": "Stage" + } + }, "ExplicitApiDefinitionBodyProdStage": { "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ExplicitApiDefinitionBodyDeploymentcb68c305fb" + "Ref": "ExplicitApiDefinitionBodyDeployment1f26996adb" }, "RestApiId": { "Ref": "ExplicitApiDefinitionBody" @@ -92,6 +100,7 @@ "paths": {}, "swagger": "2.0", "x-amazon-apigateway-binary-media-types": [ + "image/jpeg", "image/jpg", { "Fn::Join": [ @@ -106,19 +115,20 @@ ] }, "BinaryMediaTypes": [ - "image/jpg", + "image~1jpeg", + "image~1jpg", { "Fn::Join": [ - "/", + "~1", [ "image", "png" ] ] }, - "image/gif" + "image~1gif" ] } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/aws-cn/api_with_binary_media_types.json b/tests/translator/output/aws-cn/api_with_binary_media_types.json index 701e261d64..44362e0e56 100644 --- a/tests/translator/output/aws-cn/api_with_binary_media_types.json +++ b/tests/translator/output/aws-cn/api_with_binary_media_types.json @@ -1,4 +1,10 @@ { + "Parameters": { + "BMT": { + "Default": "image~1jpg", + "Type": "String" + } + }, "Resources": { "ImplicitApiFunction": { "Type": "AWS::Lambda::Function", @@ -47,16 +53,6 @@ } } }, - "ExplicitApiDeploymentf117c932f7": { - "Type": "AWS::ApiGateway::Deployment", - "Properties": { - "RestApiId": { - "Ref": "ExplicitApi" - }, - "Description": "RestApi deployment id: f117c932f75cfa87d23dfed64e9430d0081ef289", - "StageName": "Stage" - } - }, "ImplicitApiFunctionGetHtmlPermissionTest": { "Type": "AWS::Lambda::Permission", "Properties": { @@ -103,17 +99,18 @@ "Type": "AWS::ApiGateway::RestApi", "Properties": { "BinaryMediaTypes": [ - "image/jpg", + "image~1gif", { "Fn::Join": [ - "/", + "~1", [ "image", "png" ] ] }, - "image/gif" + "application~1octet-stream", + "image~1jpg" ], "EndpointConfiguration": { "Types": [ @@ -145,7 +142,7 @@ "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ServerlessRestApiDeploymentd244838cf7" + "Ref": "ServerlessRestApiDeploymentcc75b253a7" }, "RestApiId": { "Ref": "ServerlessRestApi" @@ -153,13 +150,23 @@ "StageName": "Prod" } }, - "ServerlessRestApiDeploymentd244838cf7": { + "ServerlessRestApiDeploymentcc75b253a7": { "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { "Ref": "ServerlessRestApi" }, - "Description": "RestApi deployment id: d244838cf7da9d22daa2df81b63056f8b578f711", + "Description": "RestApi deployment id: cc75b253a750cc6450a4de493f56a526d23b52d4", + "StageName": "Stage" + } + }, + "ExplicitApiDeploymentf117c932f7": { + "Type": "AWS::ApiGateway::Deployment", + "Properties": { + "RestApiId": { + "Ref": "ExplicitApi" + }, + "Description": "RestApi deployment id: f117c932f75cfa87d23dfed64e9430d0081ef289", "StageName": "Stage" } }, @@ -189,7 +196,7 @@ }, "swagger": "2.0", "x-amazon-apigateway-binary-media-types": [ - "image/jpg", + "image/gif", { "Fn::Join": [ "/", @@ -202,10 +209,10 @@ ] }, "BinaryMediaTypes": [ - "image/jpg", + "image~1gif", { "Fn::Join": [ - "/", + "~1", [ "image", "png" @@ -224,4 +231,4 @@ } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/aws-cn/api_with_binary_media_types_definition_body.json b/tests/translator/output/aws-cn/api_with_binary_media_types_definition_body.json index 0e654720a9..75b031c547 100644 --- a/tests/translator/output/aws-cn/api_with_binary_media_types_definition_body.json +++ b/tests/translator/output/aws-cn/api_with_binary_media_types_definition_body.json @@ -1,20 +1,16 @@ { + "Parameters": { + "BMT": { + "Default": "image~1jpeg", + "Type": "String" + } + }, "Resources": { - "ExplicitApiManagedSwaggerDeployment51ba79c0e7": { - "Type": "AWS::ApiGateway::Deployment", - "Properties": { - "RestApiId": { - "Ref": "ExplicitApiManagedSwagger" - }, - "Description": "RestApi deployment id: 51ba79c0e7185c019037fb7f9f9f2235547af57b", - "StageName": "Stage" - } - }, "ExplicitApiManagedSwaggerProdStage": { "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ExplicitApiManagedSwaggerDeployment51ba79c0e7" + "Ref": "ExplicitApiManagedSwaggerDeploymentfe9c2c09a2" }, "RestApiId": { "Ref": "ExplicitApiManagedSwagger" @@ -22,13 +18,13 @@ "StageName": "Prod" } }, - "ExplicitApiDefinitionBodyDeploymentcb68c305fb": { + "ExplicitApiManagedSwaggerDeploymentfe9c2c09a2": { "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { - "Ref": "ExplicitApiDefinitionBody" + "Ref": "ExplicitApiManagedSwagger" }, - "Description": "RestApi deployment id: cb68c305fb80d8e723c6c5d51a7b62462a25f5ba", + "Description": "RestApi deployment id: fe9c2c09a27ce00c2fa53d5a491cf343e6a3278e", "StageName": "Stage" } }, @@ -39,6 +35,7 @@ "paths": {}, "swagger": "2.0", "x-amazon-apigateway-binary-media-types": [ + "image/jpeg", "image/jpg", { "Fn::Join": [ @@ -53,17 +50,18 @@ ] }, "BinaryMediaTypes": [ - "image/jpg", + "image~1jpeg", + "image~1jpg", { "Fn::Join": [ - "/", + "~1", [ "image", "png" ] ] }, - "application/json" + "application~1json" ], "EndpointConfiguration": { "Types": [ @@ -75,11 +73,21 @@ } } }, + "ExplicitApiDefinitionBodyDeployment1f26996adb": { + "Type": "AWS::ApiGateway::Deployment", + "Properties": { + "RestApiId": { + "Ref": "ExplicitApiDefinitionBody" + }, + "Description": "RestApi deployment id: 1f26996adbe5077359ecb2bb0688a9cc0ae8e4fc", + "StageName": "Stage" + } + }, "ExplicitApiDefinitionBodyProdStage": { "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ExplicitApiDefinitionBodyDeploymentcb68c305fb" + "Ref": "ExplicitApiDefinitionBodyDeployment1f26996adb" }, "RestApiId": { "Ref": "ExplicitApiDefinitionBody" @@ -100,6 +108,7 @@ "paths": {}, "swagger": "2.0", "x-amazon-apigateway-binary-media-types": [ + "image/jpeg", "image/jpg", { "Fn::Join": [ @@ -114,17 +123,18 @@ ] }, "BinaryMediaTypes": [ - "image/jpg", + "image~1jpeg", + "image~1jpg", { "Fn::Join": [ - "/", + "~1", [ "image", "png" ] ] }, - "image/gif" + "image~1gif" ], "EndpointConfiguration": { "Types": [ @@ -137,4 +147,4 @@ } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/aws-us-gov/api_with_binary_media_types.json b/tests/translator/output/aws-us-gov/api_with_binary_media_types.json index 5ca64cccb7..7ebb493c39 100644 --- a/tests/translator/output/aws-us-gov/api_with_binary_media_types.json +++ b/tests/translator/output/aws-us-gov/api_with_binary_media_types.json @@ -1,4 +1,10 @@ { + "Parameters": { + "BMT": { + "Default": "image~1jpg", + "Type": "String" + } + }, "Resources": { "ImplicitApiFunction": { "Type": "AWS::Lambda::Function", @@ -103,17 +109,18 @@ "Type": "AWS::ApiGateway::RestApi", "Properties": { "BinaryMediaTypes": [ - "image/jpg", + "image~1gif", { "Fn::Join": [ - "/", + "~1", [ "image", "png" ] ] }, - "image/gif" + "application~1octet-stream", + "image~1jpg" ], "EndpointConfiguration": { "Types": [ @@ -145,7 +152,7 @@ "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ServerlessRestApiDeploymentea8de7b9f0" + "Ref": "ServerlessRestApiDeployment9746c8328e" }, "RestApiId": { "Ref": "ServerlessRestApi" @@ -153,13 +160,13 @@ "StageName": "Prod" } }, - "ServerlessRestApiDeploymentea8de7b9f0": { + "ServerlessRestApiDeployment9746c8328e": { "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { "Ref": "ServerlessRestApi" }, - "Description": "RestApi deployment id: ea8de7b9f04eec70c7dba6863ed6ff707ea129de", + "Description": "RestApi deployment id: 9746c8328eca81d7b29fed89d9203021b42242ec", "StageName": "Stage" } }, @@ -189,7 +196,7 @@ }, "swagger": "2.0", "x-amazon-apigateway-binary-media-types": [ - "image/jpg", + "image/gif", { "Fn::Join": [ "/", @@ -202,10 +209,10 @@ ] }, "BinaryMediaTypes": [ - "image/jpg", + "image~1gif", { "Fn::Join": [ - "/", + "~1", [ "image", "png" @@ -224,4 +231,4 @@ } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/aws-us-gov/api_with_binary_media_types_definition_body.json b/tests/translator/output/aws-us-gov/api_with_binary_media_types_definition_body.json index 0e654720a9..75b031c547 100644 --- a/tests/translator/output/aws-us-gov/api_with_binary_media_types_definition_body.json +++ b/tests/translator/output/aws-us-gov/api_with_binary_media_types_definition_body.json @@ -1,20 +1,16 @@ { + "Parameters": { + "BMT": { + "Default": "image~1jpeg", + "Type": "String" + } + }, "Resources": { - "ExplicitApiManagedSwaggerDeployment51ba79c0e7": { - "Type": "AWS::ApiGateway::Deployment", - "Properties": { - "RestApiId": { - "Ref": "ExplicitApiManagedSwagger" - }, - "Description": "RestApi deployment id: 51ba79c0e7185c019037fb7f9f9f2235547af57b", - "StageName": "Stage" - } - }, "ExplicitApiManagedSwaggerProdStage": { "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ExplicitApiManagedSwaggerDeployment51ba79c0e7" + "Ref": "ExplicitApiManagedSwaggerDeploymentfe9c2c09a2" }, "RestApiId": { "Ref": "ExplicitApiManagedSwagger" @@ -22,13 +18,13 @@ "StageName": "Prod" } }, - "ExplicitApiDefinitionBodyDeploymentcb68c305fb": { + "ExplicitApiManagedSwaggerDeploymentfe9c2c09a2": { "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { - "Ref": "ExplicitApiDefinitionBody" + "Ref": "ExplicitApiManagedSwagger" }, - "Description": "RestApi deployment id: cb68c305fb80d8e723c6c5d51a7b62462a25f5ba", + "Description": "RestApi deployment id: fe9c2c09a27ce00c2fa53d5a491cf343e6a3278e", "StageName": "Stage" } }, @@ -39,6 +35,7 @@ "paths": {}, "swagger": "2.0", "x-amazon-apigateway-binary-media-types": [ + "image/jpeg", "image/jpg", { "Fn::Join": [ @@ -53,17 +50,18 @@ ] }, "BinaryMediaTypes": [ - "image/jpg", + "image~1jpeg", + "image~1jpg", { "Fn::Join": [ - "/", + "~1", [ "image", "png" ] ] }, - "application/json" + "application~1json" ], "EndpointConfiguration": { "Types": [ @@ -75,11 +73,21 @@ } } }, + "ExplicitApiDefinitionBodyDeployment1f26996adb": { + "Type": "AWS::ApiGateway::Deployment", + "Properties": { + "RestApiId": { + "Ref": "ExplicitApiDefinitionBody" + }, + "Description": "RestApi deployment id: 1f26996adbe5077359ecb2bb0688a9cc0ae8e4fc", + "StageName": "Stage" + } + }, "ExplicitApiDefinitionBodyProdStage": { "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ExplicitApiDefinitionBodyDeploymentcb68c305fb" + "Ref": "ExplicitApiDefinitionBodyDeployment1f26996adb" }, "RestApiId": { "Ref": "ExplicitApiDefinitionBody" @@ -100,6 +108,7 @@ "paths": {}, "swagger": "2.0", "x-amazon-apigateway-binary-media-types": [ + "image/jpeg", "image/jpg", { "Fn::Join": [ @@ -114,17 +123,18 @@ ] }, "BinaryMediaTypes": [ - "image/jpg", + "image~1jpeg", + "image~1jpg", { "Fn::Join": [ - "/", + "~1", [ "image", "png" ] ] }, - "image/gif" + "image~1gif" ], "EndpointConfiguration": { "Types": [ @@ -137,4 +147,4 @@ } } } -} \ No newline at end of file +}