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
21 changes: 21 additions & 0 deletions samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def _construct_rest_api(self):
:rtype: model.apigateway.ApiGatewayRestApi
"""
rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on, attributes=self.resource_attributes)
# NOTE: For backwards compatibility we need to retain BinaryMediaTypes on the CloudFormation Property
# Removing this and only setting x-amazon-apigateway-binary-media-types results in other issues.
rest_api.BinaryMediaTypes = self.binary_media
rest_api.MinimumCompressionSize = self.minimum_compression_size

Expand All @@ -104,6 +106,7 @@ def _construct_rest_api(self):
self._add_cors()
self._add_auth()
self._add_gateway_responses()
self._add_binary_media_types()

if self.definition_uri:
rest_api.BodyS3Location = self._construct_body_s3_dict()
Expand Down Expand Up @@ -257,6 +260,24 @@ def _add_cors(self):
# Assign the Swagger back to template
self.definition_body = editor.swagger

def _add_binary_media_types(self):
"""
Add binary media types to Swagger
"""

if not self.binary_media:
return

# We don't raise an error here like we do for similar cases because that would be backwards incompatible
if self.binary_media and not self.definition_body:
return

editor = SwaggerEditor(self.definition_body)
editor.add_binary_media_types(self.binary_media)

# Assign the Swagger back to template
self.definition_body = editor.swagger

def _add_auth(self):
"""
Add Auth configuration to the Swagger file, if necessary
Expand Down
4 changes: 4 additions & 0 deletions samtranslator/swagger/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SwaggerEditor(object):

_OPTIONS_METHOD = "options"
_X_APIGW_INTEGRATION = 'x-amazon-apigateway-integration'
_X_APIGW_BINARY_MEDIA_TYPES = 'x-amazon-apigateway-binary-media-types'
_CONDITIONAL_IF = "Fn::If"
_X_APIGW_GATEWAY_RESPONSES = 'x-amazon-apigateway-gateway-responses'
_X_ANY_METHOD = 'x-amazon-apigateway-any-method'
Expand Down Expand Up @@ -254,6 +255,9 @@ def add_cors(self, path, allowed_origins, allowed_headers=None, allowed_methods=
max_age,
allow_credentials)

def add_binary_media_types(self, binary_media_types):
self._doc[self._X_APIGW_BINARY_MEDIA_TYPES] = binary_media_types

def _options_method_response_for_cors(self, allowed_origins, allowed_headers=None, allowed_methods=None,
max_age=None, allow_credentials=None):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Globals:
Api:
BinaryMediaTypes:
- image/jpg
- {"Fn::Join": ["/", ["image", "png"]]}

Resources:
ExplicitApiManagedSwagger:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
BinaryMediaTypes:
- image/gif

ExplicitApiDefinitionBody:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
BinaryMediaTypes:
- application/json
DefinitionBody: {
"paths": {},
"swagger": "2.0",
}
22 changes: 17 additions & 5 deletions tests/translator/output/api_with_binary_media_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"ImplicitApiFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.gethtml",
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "member_portal.zip"
},
"Handler": "index.gethtml",
"Role": {
"Fn::GetAtt": [
"ImplicitApiFunctionRole",
Expand Down Expand Up @@ -137,21 +137,21 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ServerlessRestApiDeployment62b96c1a61"
"Ref": "ServerlessRestApiDeployment706c6ba929"
},
"RestApiId": {
"Ref": "ServerlessRestApi"
},
"StageName": "Prod"
}
},
"ServerlessRestApiDeployment62b96c1a61": {
"ServerlessRestApiDeployment706c6ba929": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ServerlessRestApi"
},
"Description": "RestApi deployment id: 62b96c1a611878eefb13e8ef66dbc71b9ba3dd19",
"Description": "RestApi deployment id: 706c6ba9298c730536b2f4c51409cf33e5d616d4",
"StageName": "Stage"
}
},
Expand Down Expand Up @@ -179,7 +179,19 @@
}
}
},
"swagger": "2.0"
"swagger": "2.0",
"x-amazon-apigateway-binary-media-types": [
"image/jpg",
{
"Fn::Join": [
"/",
[
"image",
"png"
]
]
}
]
},
"BinaryMediaTypes": [
"image/jpg",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
"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"
},
"RestApiId": {
"Ref": "ExplicitApiManagedSwagger"
},
"StageName": "Prod"
}
},
"ExplicitApiDefinitionBodyDeploymentcb68c305fb": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ExplicitApiDefinitionBody"
},
"Description": "RestApi deployment id: cb68c305fb80d8e723c6c5d51a7b62462a25f5ba",
"StageName": "Stage"
}
},
"ExplicitApiDefinitionBody": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"paths": {},
"swagger": "2.0",
"x-amazon-apigateway-binary-media-types": [
"image/jpg",
{
"Fn::Join": [
"/",
[
"image",
"png"
]
]
},
"application/json"
]
},
"BinaryMediaTypes": [
"image/jpg",
{
"Fn::Join": [
"/",
[
"image",
"png"
]
]
},
"application/json"
]
}
},
"ExplicitApiDefinitionBodyProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ExplicitApiDefinitionBodyDeploymentcb68c305fb"
},
"RestApiId": {
"Ref": "ExplicitApiDefinitionBody"
},
"StageName": "Prod"
}
},
"ExplicitApiManagedSwagger": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"info": {
"version": "1.0",
"title": {
"Ref": "AWS::StackName"
}
},
"paths": {},
"swagger": "2.0",
"x-amazon-apigateway-binary-media-types": [
"image/jpg",
{
"Fn::Join": [
"/",
[
"image",
"png"
]
]
},
"image/gif"
]
},
"BinaryMediaTypes": [
"image/jpg",
{
"Fn::Join": [
"/",
[
"image",
"png"
]
]
},
"image/gif"
]
}
}
}
}
42 changes: 27 additions & 15 deletions tests/translator/output/aws-cn/api_with_binary_media_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"ImplicitApiFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.gethtml",
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "member_portal.zip"
},
"Handler": "index.gethtml",
"Role": {
"Fn::GetAtt": [
"ImplicitApiFunctionRole",
Expand Down Expand Up @@ -47,6 +47,16 @@
}
}
},
"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 @@ -135,31 +145,21 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ServerlessRestApiDeploymentcb4fb12558"
"Ref": "ServerlessRestApiDeploymentd244838cf7"
},
"RestApiId": {
"Ref": "ServerlessRestApi"
},
"StageName": "Prod"
}
},
"ServerlessRestApiDeploymentcb4fb12558": {
"ServerlessRestApiDeploymentd244838cf7": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ServerlessRestApi"
},
"Description": "RestApi deployment id: cb4fb1255811b7b6a25dd35f23ee7ad133003b89",
"StageName": "Stage"
}
},
"ExplicitApiDeploymentf117c932f7": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ExplicitApi"
},
"Description": "RestApi deployment id: f117c932f75cfa87d23dfed64e9430d0081ef289",
"Description": "RestApi deployment id: d244838cf7da9d22daa2df81b63056f8b578f711",
"StageName": "Stage"
}
},
Expand Down Expand Up @@ -187,7 +187,19 @@
}
}
},
"swagger": "2.0"
"swagger": "2.0",
"x-amazon-apigateway-binary-media-types": [
"image/jpg",
{
"Fn::Join": [
"/",
[
"image",
"png"
]
]
}
]
},
"BinaryMediaTypes": [
"image/jpg",
Expand Down
Loading