Merge lp:~cjwatson/lazr.restful/double-closing-brace into lp:lazr.restful

Proposed by Colin Watson
Status: Merged
Merged at revision: 221
Proposed branch: lp:~cjwatson/lazr.restful/double-closing-brace
Merge into: lp:lazr.restful
Diff against target: 58 lines (+14/-5)
3 files modified
src/lazr/restful/NEWS.txt (+6/-0)
src/lazr/restful/_resource.py (+2/-3)
src/lazr/restful/docs/webservice.txt (+6/-2)
To merge this branch: bzr merge lp:~cjwatson/lazr.restful/double-closing-brace
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+355847@code.launchpad.net

Commit message

Fix double closing brace when encoding the result of a custom operation where the result has an adapter to ICollection.

Description of the change

Batch encoding is very weird; BatchingResourceMixin.batch intentionally omits the closing brace, and the caller is supposed to add it after possibly adding other parameters. This means that it's important for overridden implementations of the batch method in subclasses not to add the closing brace, as that violates the implied contract; only methods other than batch are allowed to add it. We were violating that rule in one code path, which resulted in a double closing brace.

This previously went unnoticed because of an overly-liberal doctest (which I had to fix anyway in order to cope with varying dict iteration order) and I think also because Launchpad tends to declare operations as having a return_type providing ICollectionField rather than directly returning something that can be adapted to ICollection.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/lazr/restful/NEWS.txt'
--- src/lazr/restful/NEWS.txt 2018-02-21 15:18:34 +0000
+++ src/lazr/restful/NEWS.txt 2018-09-28 15:11:57 +0000
@@ -2,6 +2,12 @@
2NEWS for lazr.restful2NEWS for lazr.restful
3=====================3=====================
44
50.20.2
6======
7
8Fix double closing brace when encoding the result of a custom operation
9where the result has an adapter to ICollection.
10
50.20.1 (2018-02-21)110.20.1 (2018-02-21)
6===================12===================
713
814
=== modified file 'src/lazr/restful/_resource.py'
--- src/lazr/restful/_resource.py 2016-02-17 01:07:21 +0000
+++ src/lazr/restful/_resource.py 2018-09-28 15:11:57 +0000
@@ -1801,7 +1801,7 @@
1801 self.request.response.setHeader('Content-Type', media_type)1801 self.request.response.setHeader('Content-Type', media_type)
1802 return result1802 return result
18031803
1804 result = self.batch(entries)1804 result = self.batch(entries) + '}'
18051805
1806 self.request.response.setHeader('Content-type', self.JSON_TYPE)1806 self.request.response.setHeader('Content-type', self.JSON_TYPE)
1807 return result1807 return result
@@ -1818,8 +1818,7 @@
1818 request = self.request1818 request = self.request
1819 result = super(CollectionResource, self).batch(entries, request)1819 result = super(CollectionResource, self).batch(entries, request)
1820 result += (1820 result += (
1821 ', "resource_type_link" : ' + simplejson.dumps(self.type_url)1821 ', "resource_type_link" : ' + simplejson.dumps(self.type_url))
1822 + '}')
1823 return result1822 return result
18241823
1825 @property1824 @property
18261825
=== modified file 'src/lazr/restful/docs/webservice.txt'
--- src/lazr/restful/docs/webservice.txt 2016-02-17 01:07:21 +0000
+++ src/lazr/restful/docs/webservice.txt 2018-09-28 15:11:57 +0000
@@ -1585,8 +1585,12 @@
1585batch from the collection.1585batch from the collection.
15861586
1587 >>> request, operation = make_dummy_operation_request(DishSet())1587 >>> request, operation = make_dummy_operation_request(DishSet())
1588 >>> operation()1588 >>> response = operation()
1589 '{"total_size": ..., "start": ...}'1589 >>> for key, value in sorted(simplejson.loads(response).items()):
1590 ... print '%s: %s' % (key, value)
1591 entries: ...
1592 start: ...
1593 total_size: ...
15901594
1591If the return value can't be converted into JSON, you'll get an1595If the return value can't be converted into JSON, you'll get an
1592exception.1596exception.

Subscribers

People subscribed via source and target branches