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 e745d6b

Browse files
authored
Ensured that patching a To-Many relationship correctly raises request error (#1251)
1 parent cecd31f commit e745d6b

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

‎CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ any parts of the framework not mentioned in the documentation should generally b
1313
### Fixed
1414

1515
* Handled zero as a valid ID for resource (regression since 6.1.0)
16+
* Ensured that patching a To-Many relationship with the `RelationshipView` correctly raises request error when passing in `None`.
17+
For emptying a To-Many relationship an empty array should be used as per [JSON:API spec](https://jsonapi.org/format/#crud-updating-to-many-relationships)
1618

1719
### Added
1820

‎example/tests/test_views.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django.test import RequestFactory, override_settings
44
from django.utils import timezone
5+
from rest_framework import status
56
from rest_framework.exceptions import NotFound
67
from rest_framework.request import Request
78
from rest_framework.reverse import reverse
@@ -174,16 +175,30 @@ def test_patch_one_to_many_relationship(self):
174175
response = self.client.get(url)
175176
assert response.data == request_data["data"]
176177

177-
def test_patch_one_to_many_relaitonship_with_none(self):
178+
def test_patch_one_to_many_relaitonship_with_empty(self):
178179
url = f"/blogs/{self.first_entry.id}/relationships/entry_set"
179-
request_data = {"data": None}
180+
181+
request_data = {"data": []}
180182
response = self.client.patch(url, data=request_data)
181-
assert response.status_code == 200, response.content.decode()
183+
assert response.status_code == status.HTTP_200_OK
182184
assert response.data == []
183185

184186
response = self.client.get(url)
185187
assert response.data == []
186188

189+
def test_patch_one_to_many_relaitonship_with_none(self):
190+
"""
191+
None for a to many relationship is invalid and should return a request error.
192+
193+
see https://jsonapi.org/format/#crud-updating-to-many-relationships
194+
"""
195+
196+
url = f"/blogs/{self.first_entry.id}/relationships/entry_set"
197+
198+
request_data = {"data": None}
199+
response = self.client.patch(url, data=request_data)
200+
assert response.status_code == status.HTTP_400_BAD_REQUEST
201+
187202
def test_patch_many_to_many_relationship(self):
188203
url = f"/entries/{self.first_entry.id}/relationships/authors"
189204
request_data = {

‎rest_framework_json_api/parsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def parse_data(self, result, parser_context):
9898
"Received data contains one or more malformed JSON:API "
9999
"Resource Identifier Object(s)"
100100
)
101-
elif not (data.get("id") and data.get("type")):
101+
elif isinstance(data, dict) andnot (data.get("id") and data.get("type")):
102102
raise ParseError(
103103
"Received data is not a valid JSON:API Resource Identifier Object"
104104
)

0 commit comments

Comments
(0)

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