Skip to content

Commit 12396ea

Browse files
committed
fix: binary media types bug fix (#1043)
1 parent 0773ce5 commit 12396ea

10 files changed

+190
-124
lines changed

samtranslator/model/sam_resources.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ def to_cloudformation(self, **kwargs):
474474
"""
475475
resources = []
476476

477+
intrinsics_resolver = kwargs["intrinsics_resolver"]
478+
self.BinaryMediaTypes = intrinsics_resolver.resolve_parameter_refs(self.BinaryMediaTypes)
479+
477480
api_generator = ApiGenerator(self.logical_id,
478481
self.CacheClusterEnabled,
479482
self.CacheClusterSize,

samtranslator/swagger/swagger.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import json
23
import re
34
from six import string_types
45

@@ -257,7 +258,8 @@ def add_cors(self, path, allowed_origins, allowed_headers=None, allowed_methods=
257258
allow_credentials)
258259

259260
def add_binary_media_types(self, binary_media_types):
260-
self._doc[self._X_APIGW_BINARY_MEDIA_TYPES] = binary_media_types
261+
bmt = json.loads(json.dumps(binary_media_types).replace('~1', '/'))
262+
self._doc[self._X_APIGW_BINARY_MEDIA_TYPES] = bmt
261263

262264
def _options_method_response_for_cors(self, allowed_origins, allowed_headers=None, allowed_methods=None,
263265
max_age=None, allow_credentials=None):

tests/translator/input/api_with_binary_media_types.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
Parameters:
2+
BMT:
3+
Type: String
4+
Default: 'image~1jpg'
15
Globals:
26
Api:
37
BinaryMediaTypes:
4-
- image/jpg
5-
- {"Fn::Join": ["/", ["image", "png"]]}
8+
- image~1gif
9+
- {"Fn::Join": ["~1", ["image", "png"]]}
610

711
Resources:
812
ImplicitApiFunction:
@@ -24,4 +28,5 @@ Resources:
2428
StageName: Prod
2529
DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json
2630
BinaryMediaTypes:
27-
- image/gif
31+
- application~1octet-stream
32+
- !Ref BMT

tests/translator/input/api_with_binary_media_types_definition_body.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1+
Parameters:
2+
BMT:
3+
Type: String
4+
Default: image~1jpeg
15
Globals:
26
Api:
37
BinaryMediaTypes:
4-
- image/jpg
5-
- {"Fn::Join": ["/", ["image", "png"]]}
8+
- !Ref BMT
9+
- image~1jpg
10+
- {"Fn::Join": ["~1", ["image", "png"]]}
611

712
Resources:
813
ExplicitApiManagedSwagger:
914
Type: AWS::Serverless::Api
1015
Properties:
1116
StageName: Prod
1217
BinaryMediaTypes:
13-
- image/gif
18+
- image~1gif
1419

1520
ExplicitApiDefinitionBody:
1621
Type: AWS::Serverless::Api
1722
Properties:
1823
StageName: Prod
1924
BinaryMediaTypes:
20-
- application/json
25+
- application~1json
2126
DefinitionBody: {
2227
"paths": {},
2328
"swagger": "2.0",

tests/translator/output/api_with_binary_media_types.json

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
2+
"Parameters": {
3+
"BMT": {
4+
"Default": "image~1jpg",
5+
"Type": "String"
6+
}
7+
},
28
"Resources": {
39
"ImplicitApiFunction": {
410
"Type": "AWS::Lambda::Function",
@@ -47,16 +53,6 @@
4753
}
4854
}
4955
},
50-
"ExplicitApiDeploymentf117c932f7": {
51-
"Type": "AWS::ApiGateway::Deployment",
52-
"Properties": {
53-
"RestApiId": {
54-
"Ref": "ExplicitApi"
55-
},
56-
"Description": "RestApi deployment id: f117c932f75cfa87d23dfed64e9430d0081ef289",
57-
"StageName": "Stage"
58-
}
59-
},
6056
"ImplicitApiFunctionGetHtmlPermissionTest": {
6157
"Type": "AWS::Lambda::Permission",
6258
"Properties": {
@@ -103,17 +99,18 @@
10399
"Type": "AWS::ApiGateway::RestApi",
104100
"Properties": {
105101
"BinaryMediaTypes": [
106-
"image/jpg",
102+
"image~1gif",
107103
{
108104
"Fn::Join": [
109-
"/",
105+
"~1",
110106
[
111107
"image",
112108
"png"
113109
]
114110
]
115111
},
116-
"image/gif"
112+
"application~1octet-stream",
113+
"image~1jpg"
117114
],
118115
"BodyS3Location": {
119116
"Bucket": "sam-demo-bucket",
@@ -137,21 +134,31 @@
137134
"Type": "AWS::ApiGateway::Stage",
138135
"Properties": {
139136
"DeploymentId": {
140-
"Ref": "ServerlessRestApiDeployment706c6ba929"
137+
"Ref": "ServerlessRestApiDeployment4f3fcf6bd1"
141138
},
142139
"RestApiId": {
143140
"Ref": "ServerlessRestApi"
144141
},
145142
"StageName": "Prod"
146143
}
147144
},
148-
"ServerlessRestApiDeployment706c6ba929": {
145+
"ExplicitApiDeploymentf117c932f7": {
146+
"Type": "AWS::ApiGateway::Deployment",
147+
"Properties": {
148+
"RestApiId": {
149+
"Ref": "ExplicitApi"
150+
},
151+
"Description": "RestApi deployment id: f117c932f75cfa87d23dfed64e9430d0081ef289",
152+
"StageName": "Stage"
153+
}
154+
},
155+
"ServerlessRestApiDeployment4f3fcf6bd1": {
149156
"Type": "AWS::ApiGateway::Deployment",
150157
"Properties": {
151158
"RestApiId": {
152159
"Ref": "ServerlessRestApi"
153160
},
154-
"Description": "RestApi deployment id: 706c6ba9298c730536b2f4c51409cf33e5d616d4",
161+
"Description": "RestApi deployment id: 4f3fcf6bd13686961885c787e3a117b084ec6c3a",
155162
"StageName": "Stage"
156163
}
157164
},
@@ -181,7 +188,7 @@
181188
},
182189
"swagger": "2.0",
183190
"x-amazon-apigateway-binary-media-types": [
184-
"image/jpg",
191+
"image/gif",
185192
{
186193
"Fn::Join": [
187194
"/",
@@ -194,10 +201,10 @@
194201
]
195202
},
196203
"BinaryMediaTypes": [
197-
"image/jpg",
204+
"image~1gif",
198205
{
199206
"Fn::Join": [
200-
"/",
207+
"~1",
201208
[
202209
"image",
203210
"png"
@@ -208,4 +215,4 @@
208215
}
209216
}
210217
}
211-
}
218+
}

tests/translator/output/api_with_binary_media_types_definition_body.json

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
{
2+
"Parameters": {
3+
"BMT": {
4+
"Default": "image~1jpeg",
5+
"Type": "String"
6+
}
7+
},
28
"Resources": {
3-
"ExplicitApiManagedSwaggerDeployment51ba79c0e7": {
4-
"Type": "AWS::ApiGateway::Deployment",
5-
"Properties": {
6-
"RestApiId": {
7-
"Ref": "ExplicitApiManagedSwagger"
8-
},
9-
"Description": "RestApi deployment id: 51ba79c0e7185c019037fb7f9f9f2235547af57b",
10-
"StageName": "Stage"
11-
}
12-
},
139
"ExplicitApiManagedSwaggerProdStage": {
1410
"Type": "AWS::ApiGateway::Stage",
1511
"Properties": {
1612
"DeploymentId": {
17-
"Ref": "ExplicitApiManagedSwaggerDeployment51ba79c0e7"
13+
"Ref": "ExplicitApiManagedSwaggerDeploymentfe9c2c09a2"
1814
},
1915
"RestApiId": {
2016
"Ref": "ExplicitApiManagedSwagger"
2117
},
2218
"StageName": "Prod"
2319
}
2420
},
25-
"ExplicitApiDefinitionBodyDeploymentcb68c305fb": {
21+
"ExplicitApiManagedSwaggerDeploymentfe9c2c09a2": {
2622
"Type": "AWS::ApiGateway::Deployment",
2723
"Properties": {
2824
"RestApiId": {
29-
"Ref": "ExplicitApiDefinitionBody"
25+
"Ref": "ExplicitApiManagedSwagger"
3026
},
31-
"Description": "RestApi deployment id: cb68c305fb80d8e723c6c5d51a7b62462a25f5ba",
27+
"Description": "RestApi deployment id: fe9c2c09a27ce00c2fa53d5a491cf343e6a3278e",
3228
"StageName": "Stage"
3329
}
3430
},
@@ -39,6 +35,7 @@
3935
"paths": {},
4036
"swagger": "2.0",
4137
"x-amazon-apigateway-binary-media-types": [
38+
"image/jpeg",
4239
"image/jpg",
4340
{
4441
"Fn::Join": [
@@ -53,25 +50,36 @@
5350
]
5451
},
5552
"BinaryMediaTypes": [
56-
"image/jpg",
53+
"image~1jpeg",
54+
"image~1jpg",
5755
{
5856
"Fn::Join": [
59-
"/",
57+
"~1",
6058
[
6159
"image",
6260
"png"
6361
]
6462
]
6563
},
66-
"application/json"
64+
"application~1json"
6765
]
6866
}
6967
},
68+
"ExplicitApiDefinitionBodyDeployment1f26996adb": {
69+
"Type": "AWS::ApiGateway::Deployment",
70+
"Properties": {
71+
"RestApiId": {
72+
"Ref": "ExplicitApiDefinitionBody"
73+
},
74+
"Description": "RestApi deployment id: 1f26996adbe5077359ecb2bb0688a9cc0ae8e4fc",
75+
"StageName": "Stage"
76+
}
77+
},
7078
"ExplicitApiDefinitionBodyProdStage": {
7179
"Type": "AWS::ApiGateway::Stage",
7280
"Properties": {
7381
"DeploymentId": {
74-
"Ref": "ExplicitApiDefinitionBodyDeploymentcb68c305fb"
82+
"Ref": "ExplicitApiDefinitionBodyDeployment1f26996adb"
7583
},
7684
"RestApiId": {
7785
"Ref": "ExplicitApiDefinitionBody"
@@ -92,6 +100,7 @@
92100
"paths": {},
93101
"swagger": "2.0",
94102
"x-amazon-apigateway-binary-media-types": [
103+
"image/jpeg",
95104
"image/jpg",
96105
{
97106
"Fn::Join": [
@@ -106,19 +115,20 @@
106115
]
107116
},
108117
"BinaryMediaTypes": [
109-
"image/jpg",
118+
"image~1jpeg",
119+
"image~1jpg",
110120
{
111121
"Fn::Join": [
112-
"/",
122+
"~1",
113123
[
114124
"image",
115125
"png"
116126
]
117127
]
118128
},
119-
"image/gif"
129+
"image~1gif"
120130
]
121131
}
122132
}
123133
}
124-
}
134+
}

0 commit comments

Comments
 (0)