diff --git a/example/tests/test_model_viewsets.py b/example/tests/test_model_viewsets.py index 895f63e4..71859e1f 100644 --- a/example/tests/test_model_viewsets.py +++ b/example/tests/test_model_viewsets.py @@ -184,6 +184,25 @@ def test_key_in_detail_result(self): assert expected_dump == content_dump + def test_patch_requires_id(self): + """ + Verify that 'id' is required to be passed in an update request. + """ + data = { + 'data': { + 'type': 'users', + 'attributes': { + 'first-name': 'DifferentName' + } + } + } + + response = self.client.patch(self.detail_url, + content_type='application/vnd.api+json', + data=dump_json(data)) + + self.assertEqual(response.status_code, 400) + def test_key_in_post(self): """ Ensure a key is in the post. diff --git a/example/tests/test_views.py b/example/tests/test_views.py index a4f4ce75..7a1a07a8 100644 --- a/example/tests/test_views.py +++ b/example/tests/test_views.py @@ -155,6 +155,7 @@ def test_delete_relationship_overriding_with_none(self): request_data = { 'data': { 'type': 'comments', + 'id': self.second_comment.id, 'relationships': { 'author': { 'data': None diff --git a/rest_framework_json_api/parsers.py b/rest_framework_json_api/parsers.py index 30b9ad0e..dc24a2df 100644 --- a/rest_framework_json_api/parsers.py +++ b/rest_framework_json_api/parsers.py @@ -80,6 +80,8 @@ def parse(self, stream, media_type=None, parser_context=None): resource_type=resource_name ) ) + if not data.get('id') and request.method in ('PATCH', 'PUT'): + raise ParseError("The resource identifier object must contain an 'id' member") # Construct the return data parsed_data = {'id': data.get('id')}