From 5d36ac47eddf30235dc924ba796c26caa97d67f5 Mon Sep 17 00:00:00 2001 From: Tony Pilara Date: Thu, 26 May 2016 11:07:58 -0700 Subject: [PATCH 1/2] Require 'id' included in PATCH request --- example/tests/test_model_viewsets.py | 19 +++++++++++++++++++ rest_framework_json_api/parsers.py | 2 ++ 2 files changed, 21 insertions(+) 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/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')} From 51d2a2c65e74a851ce7d3c136c4ad1b71765eda5 Mon Sep 17 00:00:00 2001 From: Tony Pilara Date: Tue, 31 May 2016 10:57:40 -0700 Subject: [PATCH 2/2] Add missing 'id' to test --- example/tests/test_views.py | 1 + 1 file changed, 1 insertion(+) 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