Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,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,
Expand Down
4 changes: 3 additions & 1 deletion samtranslator/swagger/swagger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import json
import re
from six import string_types

Expand Down Expand Up @@ -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', '/'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dump it into json so all nested lists and dictionaries are expressed as a string, replace the ~1 with /, and then load back into data form.

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):
Expand Down
11 changes: 8 additions & 3 deletions tests/translator/input/api_with_binary_media_types.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -24,4 +28,5 @@ Resources:
StageName: Prod
DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json
BinaryMediaTypes:
- image/gif
- application~1octet-stream
- !Ref BMT
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
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:
Type: AWS::Serverless::Api
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",
Expand Down
47 changes: 27 additions & 20 deletions tests/translator/output/api_with_binary_media_types.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"Parameters": {
"BMT": {
"Default": "image~1jpg",
"Type": "String"
}
},
"Resources": {
"ImplicitApiFunction": {
"Type": "AWS::Lambda::Function",
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand All @@ -137,21 +134,31 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ServerlessRestApiDeployment706c6ba929"
"Ref": "ServerlessRestApiDeployment4f3fcf6bd1"
},
"RestApiId": {
"Ref": "ServerlessRestApi"
},
"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"
}
},
Expand Down Expand Up @@ -181,7 +188,7 @@
},
"swagger": "2.0",
"x-amazon-apigateway-binary-media-types": [
"image/jpg",
"image/gif",
{
"Fn::Join": [
"/",
Expand All @@ -194,10 +201,10 @@
]
},
"BinaryMediaTypes": [
"image/jpg",
"image~1gif",
{
"Fn::Join": [
"/",
"~1",
[
"image",
"png"
Expand All @@ -208,4 +215,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
{
"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"
},
"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"
}
},
Expand All @@ -39,6 +35,7 @@
"paths": {},
"swagger": "2.0",
"x-amazon-apigateway-binary-media-types": [
"image/jpeg",
"image/jpg",
{
"Fn::Join": [
Expand All @@ -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"
Expand All @@ -92,6 +100,7 @@
"paths": {},
"swagger": "2.0",
"x-amazon-apigateway-binary-media-types": [
"image/jpeg",
"image/jpg",
{
"Fn::Join": [
Expand All @@ -106,19 +115,20 @@
]
},
"BinaryMediaTypes": [
"image/jpg",
"image~1jpeg",
"image~1jpg",
{
"Fn::Join": [
"/",
"~1",
[
"image",
"png"
]
]
},
"image/gif"
"image~1gif"
]
}
}
}
}
}
Loading