Movatterモバイル変換


[0]ホーム

URL:


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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
n2ygk merged 1 commit intodjango-json-api:mainfromsliverc:patch_relationship
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsexample/tests/test_views.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -174,16 +175,30 @@ def test_patch_one_to_many_relationship(self):
response = self.client.get(url)
assert response.data == request_data["data"]

deftest_patch_one_to_many_relaitonship_with_none(self):
deftest_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 deletionrest_framework_json_api/parsers.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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")):
elifisinstance(data, dict) andnot (data.get("id") and data.get("type")):
raise ParseError(
"Received data is not a valid JSON:API Resource Identifier Object"
)
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp