Skip to content

Enforce conflict response when requested PATCH resource object is unequal to endpoint #763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Adam Wróbel <https://adamwrobel.com>
Adam Ziolkowski <[email protected]>
Alan Crosswell <[email protected]>
Anton Shutik <[email protected]>
Boris Pleshakov <[email protected]>
Christian Zosel <https://zosel.ch>
David Vogt <[email protected]>
Greg Aker <[email protected]>
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ any parts of the framework not mentioned in the documentation should generally b
* Added support for Django REST framework 3.11
* Added support for Django 3.0

### Fixed

* Ensure that `409 Conflict` is returned when processing a `PATCH` request in which the resource object’s type and id do not match the server’s endpoint properly as outlined in [JSON:API](https://jsonapi.org/format/#crud-updating-responses-409) spec.

## [3.0.0] - 2019-10-14

This release is not backwards compatible. For easy migration best upgrade first to version
Expand Down
18 changes: 18 additions & 0 deletions example/tests/test_model_viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ def test_patch_requires_id(self):

self.assertEqual(response.status_code, 400)

def test_patch_requires_correct_id(self):
"""
Verify that 'id' is the same then in url
"""
data = {
'data': {
'type': 'users',
'id': self.miles.pk + 1,
'attributes': {
'first-name': 'DifferentName'
}
}
}

response = self.client.patch(self.detail_url, data=data)

self.assertEqual(response.status_code, 409)

def test_key_in_post(self):
"""
Ensure a key is in the post.
Expand Down
11 changes: 11 additions & 0 deletions rest_framework_json_api/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ def parse(self, stream, media_type=None, parser_context=None):
if not data.get('id') and request.method in ('PATCH', 'PUT'):
raise ParseError("The resource identifier object must contain an 'id' member")

if request.method in ('PATCH', 'PUT'):
lookup_url_kwarg = view.lookup_url_kwarg or view.lookup_field
if str(data.get('id')) != str(view.kwargs[lookup_url_kwarg]):
raise exceptions.Conflict(
"The resource object's id ({data_id}) does not match url's "
"lookup id ({url_id})".format(
data_id=data.get('id'),
url_id=view.kwargs[view.lookup_field]
)
)

# Construct the return data
serializer_class = getattr(view, 'serializer_class', None)
parsed_data = {'id': data.get('id')} if 'id' in data else {}
Expand Down