Merge lp:~cjwatson/lazr.restful/no-json-binary into lp:lazr.restful

Proposed by Colin Watson
Status: Merged
Merged at revision: 286
Proposed branch: lp:~cjwatson/lazr.restful/no-json-binary
Merge into: lp:lazr.restful
Diff against target: 79 lines (+19/-12)
3 files modified
NEWS.rst (+4/-0)
src/lazr/restful/docs/webservice-marshallers.rst (+4/-7)
src/lazr/restful/marshallers.py (+11/-5)
To merge this branch: bzr merge lp:~cjwatson/lazr.restful/no-json-binary
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+396458@code.launchpad.net

Commit message

Don't attempt to JSON-decode Bytes fields read from a request.

Description of the change

Binary data can't be marshalled via JSON without additional encoding that we don't do, and lazr.restfulclient doesn't JSON-encode binary fields.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NEWS.rst'
2--- NEWS.rst 2021-01-04 11:24:14 +0000
3+++ NEWS.rst 2021-01-18 23:15:39 +0000
4@@ -11,6 +11,10 @@
5 allowing robust use of binary arguments on both Python 2 and 3
6 (bug 1116954).
7
8+Don't attempt to JSON-decode ``Bytes`` fields read from a request, since
9+binary data can't be marshalled via JSON without additional encoding that we
10+don't do, and ``lazr.restfulclient`` doesn't JSON-encode binary fields.
11+
12 0.23.0 (2020-09-28)
13 ===================
14
15
16=== modified file 'src/lazr/restful/docs/webservice-marshallers.rst'
17--- src/lazr/restful/docs/webservice-marshallers.rst 2020-09-07 09:30:30 +0000
18+++ src/lazr/restful/docs/webservice-marshallers.rst 2021-01-18 23:15:39 +0000
19@@ -478,19 +478,16 @@
20 None
21
22 When coming over the request, the value is also converted into a UTF-8
23-encoded string.
24+encoded string, with no JSON decoding.
25
26 >>> pprint_value(marshaller.marshall_from_request(u"Test"))
27 b'Test'
28 >>> pprint_value(marshaller.marshall_from_request(u'int\xe9ressant'))
29 b'int\xc3\xa9ressant'
30- >>> pprint_value(marshaller.marshall_from_request('1.0'))
31+ >>> pprint_value(marshaller.marshall_from_request(b'1.0'))
32 b'1.0'
33-
34-But again, None is returned as is.
35-
36- >>> print(marshaller.marshall_from_request('null'))
37- None
38+ >>> pprint_value(marshaller.marshall_from_request(b'"not JSON"'))
39+ b'"not JSON"'
40
41 Since multipart/form-data can be used to upload data, file-like objects
42 are read.
43
44=== modified file 'src/lazr/restful/marshallers.py'
45--- src/lazr/restful/marshallers.py 2020-09-01 13:20:54 +0000
46+++ src/lazr/restful/marshallers.py 2021-01-18 23:15:39 +0000
47@@ -290,21 +290,27 @@
48 return "%s/%s" % (absoluteURL(entry.context, self.request),
49 self.field.__name__)
50
51- def _marshall_from_request(self, value):
52- """See `SimpleFieldMarshaller`.
53+ def marshall_from_request(self, value):
54+ """See `IFieldMarshaller`.
55
56 Reads the data from file-like object, and converts non-strings into
57- one.
58+ one. This overrides `marshall_from_request` rather than
59+ implementing the `_marshall_from_request` hook method because we
60+ need to suppress the default JSON-decoding behaviour: binary data
61+ can't be marshalled via JSON without additional encoding that we
62+ don't do, and lazr.restfulclient doesn't JSON-encode binary fields.
63 """
64+ if value is None:
65+ return None
66 if safe_hasattr(value, 'seek'):
67 value.seek(0)
68 value = value.read()
69 elif not isinstance(value, (bytes, six.text_type)):
70- value = bytes(value)
71+ value = str(value).encode('UTF-8')
72 else:
73 # Leave string conversion to _marshall_from_json_data.
74 pass
75- return super(BytesFieldMarshaller, self)._marshall_from_request(value)
76+ return self._marshall_from_json_data(value)
77
78 def _marshall_from_json_data(self, value):
79 """See `SimpleFieldMarshaller`.

Subscribers

People subscribed via source and target branches