Skip to content

Commit

Permalink
Ensured that patching a To-Many relationship correctly raises request…
Browse files Browse the repository at this point in the history
… error (#1251)
  • Loading branch information
sliverc authored Sep 10, 2024
1 parent cecd31f commit e745d6b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ any parts of the framework not mentioned in the documentation should generally b
### Fixed

* Handled zero as a valid ID for resource (regression since 6.1.0)
* Ensured that patching a To-Many relationship with the `RelationshipView` correctly raises request error when passing in `None`.
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)

### Added

Expand Down
21 changes: 18 additions & 3 deletions example/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.test import RequestFactory, override_settings
from django.utils import timezone
from rest_framework import status
from rest_framework.exceptions import NotFound
from rest_framework.request import Request
from rest_framework.reverse import reverse
Expand Down Expand Up @@ -174,16 +175,30 @@ def test_patch_one_to_many_relationship(self):
response = self.client.get(url)
assert response.data == request_data["data"]

def test_patch_one_to_many_relaitonship_with_none(self):
def test_patch_one_to_many_relaitonship_with_empty(self):
url = f"/blogs/{self.first_entry.id}/relationships/entry_set"
request_data = {"data": None}

request_data = {"data": []}
response = self.client.patch(url, data=request_data)
assert response.status_code == 200, response.content.decode()
assert response.status_code == status.HTTP_200_OK
assert response.data == []

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

def test_patch_one_to_many_relaitonship_with_none(self):
"""
None for a to many relationship is invalid and should return a request error.
see https://jsonapi.org/format/#crud-updating-to-many-relationships
"""

url = f"/blogs/{self.first_entry.id}/relationships/entry_set"

request_data = {"data": None}
response = self.client.patch(url, data=request_data)
assert response.status_code == status.HTTP_400_BAD_REQUEST

def test_patch_many_to_many_relationship(self):
url = f"/entries/{self.first_entry.id}/relationships/authors"
request_data = {
Expand Down
2 changes: 1 addition & 1 deletion rest_framework_json_api/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def parse_data(self, result, parser_context):
"Received data contains one or more malformed JSON:API "
"Resource Identifier Object(s)"
)
elif not (data.get("id") and data.get("type")):
elif isinstance(data, dict) and not (data.get("id") and data.get("type")):
raise ParseError(
"Received data is not a valid JSON:API Resource Identifier Object"
)
Expand Down

0 comments on commit e745d6b

Please sign in to comment.