Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Fix 500 when no JSON is provided on PATCH (fixes #477, #516)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Oct 29, 2015
1 parent ccbe6f1 commit a45823e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
12 changes: 11 additions & 1 deletion cliquet/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,17 @@ def patch(self):
old_record = self._get_record_or_404(self.record_id)
self._raise_412_if_modified(old_record)

changes = self.request.json.get('data', {}) # May patch only perms.
try:
# data may not be present if only perms are patched.
changes = self.request.json.get('data', {})
except ValueError:
# XXX: This could be handled in Colander schema (c.f. Viewset)
# once hacks in Cornice about schemas will be removed.
error_details = {
'name': 'data',
'description': 'Provide at least one of data or permissions',
}
raise_invalid(self.request, **error_details)

updated = self.apply_changes(old_record, changes=changes)

Expand Down
5 changes: 5 additions & 0 deletions cliquet/resource/viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ def get_record_schema(self, resource_cls, method):
permissions=allowed_permissions,
name='permissions')
schema.add(permissions_node)

# XXX: If Cornice wouldn't recreate the schema on the fly.
# We could make sure using a validator that at least one of `data` or
# `permissions` is provided.

return schema

def get_view_arguments(self, endpoint_type, resource_cls, method):
Expand Down
14 changes: 12 additions & 2 deletions cliquet/tests/resource/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,19 @@ def test_modify_with_invalid_uft8_returns_400(self):
status=400)
self.assertIn('escape sequence', resp.json['message'])

def test_modify_with_empty_returns_400(self):
def test_modify_with_empty_body_returns_400(self):
self.app.patch(self.get_item_url(),
'',
headers=self.headers,
status=400)

def test_modify_protected_with_empty_body_returns_400(self):
body = {'data': MINIMALIST_RECORD}
resp = self.app.post_json('/toadstools',
body,
headers=self.headers)
record = resp.json['data']
item_url = '/toadstools/' + record['id']
self.app.patch(item_url,
headers=self.headers,
status=400)

Expand Down

0 comments on commit a45823e

Please sign in to comment.