Skip to content

Commit

Permalink
Give a clearer error message if id is not a string
Browse files Browse the repository at this point in the history
This commit causes the server to give a clearer error message if the
resource ID given in the resource object on an update request is not a
string.
  • Loading branch information
jfinkels committed Oct 6, 2016
1 parent 52c5125 commit e78a033
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Not yet released.
- :issue:`481,488`: added negation (``not``) operator for search.
- :issue:`492`: support JSON API recommended "simple" filtering.
- :issue:`508`: flush the session before postprocessors, and commit after.
- :issue:`534`: when updating a resource, give a clearer error message if the
resource ID in the JSON document is not a JSON string.
- :issue:`536`: adds support for single-table inheritance.
- :issue:`540`: correctly support models that don't have a column named "id".
- :issue:`546`: adds support for joined table inheritance.
Expand Down
7 changes: 7 additions & 0 deletions flask_restless/views/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,13 @@ def patch(self, resource_id):
detail = 'expected type {0}, not {1}'
detail = detail.format(self.collection_name, type_)
return error_response(409, detail=detail)
# Check that the ID is a string, as required by the "Resource
# Object: Identification" section of the JSON API specification.
if not isinstance(id_, str):
detail = ('The "id" element of the resource object must be a JSON'
' string: {0}')
detail = detail.format(id_)
return error_response(409, detail=detail)
if id_ != resource_id:
message = 'ID must be {0}, not {1}'.format(resource_id, id_)
return error_response(409, detail=message)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_updating.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,25 @@ def test_special_field_names(self):
assert response.status_code == 204
assert article.type == u'bar'

def test_integer_id_error_message(self):
"""Test that an integer ID in the JSON request yields an error.
For more information, see GitHub issue #534.
"""
person = self.Person(id=1)
self.session.add(person)
self.session.commit()
data = {
'data': {
'type': 'person',
'id': 1,
}
}
response = self.app.patch('/api/person/1', data=dumps(data))
check_sole_error(response, 409, ['"id" element', 'resource object',
'must be a JSON string'])


class TestProcessors(ManagerTestBase):
"""Tests for pre- and postprocessors."""
Expand Down

0 comments on commit e78a033

Please sign in to comment.