File tree Expand file tree Collapse file tree 3 files changed +38
-2
lines changed Expand file tree Collapse file tree 3 files changed +38
-2
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "type" : " bugfix" ,
3
+ "category" : " Formatter" ,
4
+ "description" : " Update JSON formatter to encode raw bytes as UTF-8."
5
+ }
Original file line number Diff line number Diff line change 10
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
11
# ANY KIND, either express or implied. See the License for the specific
12
12
# language governing permissions and limitations under the License.
13
+ import base64
13
14
import contextlib
14
15
import csv
15
16
import datetime
@@ -372,11 +373,14 @@ def operation_uses_document_types(operation_model):
372
373
373
374
374
375
def json_encoder (obj ):
375
- """JSON encoder that formats datetimes as ISO8601 format."""
376
+ """JSON encoder that formats datetimes as ISO8601 format
377
+ and encodes bytes to UTF-8 Base64 string."""
376
378
if isinstance (obj , datetime .datetime ):
377
379
return obj .isoformat ()
380
+ elif isinstance (obj , bytes ):
381
+ return base64 .b64encode (obj ).decode ("utf-8" )
378
382
else :
379
- return obj
383
+ raise TypeError ( 'Encountered unrecognized type in JSON encoder.' )
380
384
381
385
382
386
@contextlib .contextmanager
Original file line number Diff line number Diff line change 11
11
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12
12
# ANY KIND, either express or implied. See the License for the specific
13
13
# language governing permissions and limitations under the License.
14
+ import base64
15
+ import contextlib
16
+ import io
14
17
import platform
18
+ import sys
15
19
16
20
from botocore .compat import json
17
21
@@ -134,3 +138,26 @@ def test_fully_buffered_handles_io_error(self):
134
138
# we still should have called the flush() on the
135
139
# stream.
136
140
fake_closed_stream .flush .assert_called_with ()
141
+
142
+
143
+ class TestBinaryData (unittest .TestCase ):
144
+ def test_binary_data_gets_base64_encoded (self ):
145
+ args = mock .Mock (query = None )
146
+ raw_bytes = b'foo'
147
+ response = {'BinaryValue' : raw_bytes }
148
+ stdout_b = io .BytesIO ()
149
+ stdout = io .TextIOWrapper (stdout_b , newline = '\n ' )
150
+ formatter = JSONFormatter (args )
151
+
152
+ with contextlib .redirect_stdout (stdout ):
153
+ formatter ('command-name' , response , sys .stdout )
154
+ stdout .flush ()
155
+
156
+ assert (
157
+ stdout_b .getvalue ()
158
+ == (
159
+ '{\n '
160
+ f' "BinaryValue": "{ base64 .b64encode (raw_bytes ).decode ("utf-8" )} "\n '
161
+ '}\n '
162
+ ).encode ()
163
+ )
You can’t perform that action at this time.
0 commit comments