Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 96aaa13

Browse files
bpleshakovsliverc
authored andcommitted
Enforce conflict response when requested PATCH resource object is unequal to endpoint (#763)
A server MUST return 409 Conflict when processing a PATCH request in which the resource object’s type and id do not match the server’s endpoint.
1 parent e3ae420 commit 96aaa13

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

‎AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Adam Wróbel <https://adamwrobel.com>
22
Adam Ziolkowski <adam@adsized.com>
33
Alan Crosswell <alan@columbia.edu>
44
Anton Shutik <shutikanton@gmail.com>
5+
Boris Pleshakov <koordinator.kun@gmail.com>
56
Christian Zosel <https://zosel.ch>
67
David Vogt <david.vogt@adfinis-sygroup.ch>
78
Greg Aker <greg@gregaker.net>

‎CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ any parts of the framework not mentioned in the documentation should generally b
1616
* Added support for Django REST framework 3.11
1717
* Added support for Django 3.0
1818

19+
### Fixed
20+
21+
* 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.
22+
1923
## [3.0.0] - 2019年10月14日
2024

2125
This release is not backwards compatible. For easy migration best upgrade first to version

‎example/tests/test_model_viewsets.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,24 @@ def test_patch_requires_id(self):
185185

186186
self.assertEqual(response.status_code, 400)
187187

188+
def test_patch_requires_correct_id(self):
189+
"""
190+
Verify that 'id' is the same then in url
191+
"""
192+
data = {
193+
'data': {
194+
'type': 'users',
195+
'id': self.miles.pk + 1,
196+
'attributes': {
197+
'first-name': 'DifferentName'
198+
}
199+
}
200+
}
201+
202+
response = self.client.patch(self.detail_url, data=data)
203+
204+
self.assertEqual(response.status_code, 409)
205+
188206
def test_key_in_post(self):
189207
"""
190208
Ensure a key is in the post.

‎rest_framework_json_api/parsers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ def parse(self, stream, media_type=None, parser_context=None):
139139
if not data.get('id') and request.method in ('PATCH', 'PUT'):
140140
raise ParseError("The resource identifier object must contain an 'id' member")
141141

142+
if request.method in ('PATCH', 'PUT'):
143+
lookup_url_kwarg = view.lookup_url_kwarg or view.lookup_field
144+
if str(data.get('id')) != str(view.kwargs[lookup_url_kwarg]):
145+
raise exceptions.Conflict(
146+
"The resource object's id ({data_id}) does not match url's "
147+
"lookup id ({url_id})".format(
148+
data_id=data.get('id'),
149+
url_id=view.kwargs[view.lookup_field]
150+
)
151+
)
152+
142153
# Construct the return data
143154
serializer_class = getattr(view, 'serializer_class', None)
144155
parsed_data = {'id': data.get('id')} if 'id' in data else {}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /