Skip to content

Ensured that patching a To-Many relationship correctly raises request error #1251

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 1 commit into from
Sep 10, 2024
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
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
Loading