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 7d2970a

Browse files
slivercn2ygk
andauthored
Avoided error when using include query parameter on related urls (#914)
This is a regression from version 4.1.0. Co-authored-by: Alan Crosswell <alan@columbia.edu>
1 parent 36a60a3 commit 7d2970a

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ any parts of the framework not mentioned in the documentation should generally b
2525
* Deprecated default `format_type` argument of `rest_framework_json_api.utils.format_value`. Use `rest_framework_json_api.utils.format_field_name` or specify specifc `format_type` instead.
2626
* Deprecated `format_type` argument of `rest_framework_json_api.utils.format_link_segment`. Use `format_value` instead.
2727

28+
### Fixed
29+
30+
* Avoided error when using `include` query parameter on related urls (a regression since 4.1.0)
31+
2832
## [4.1.0] - 2021年03月08日
2933

3034
### Added

‎example/tests/integration/test_browsable_api.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ def test_browsable_api_with_included_serializers(single_entry, client):
1818
)
1919

2020

21+
def test_browsable_api_on_related_url(author, client):
22+
url = reverse("author-related", kwargs={"pk": author.pk, "related_field": "bio"})
23+
response = client.get(url, data={"format": "api"})
24+
content = str(response.content)
25+
assert response.status_code == 200
26+
assert re.search(r"JSON:API includes", content)
27+
assert re.search(
28+
r'<input type="checkbox" name="includes" [^>]* value="metadata"', content
29+
)
30+
31+
2132
def test_browsable_api_with_no_included_serializers(client):
2233
response = client.get(reverse("projecttype-list", kwargs={"format": "api"}))
2334
content = str(response.content)

‎example/tests/test_views.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def test_retrieve_related_single_reverse_lookup(self):
432432
url = reverse(
433433
"author-related", kwargs={"pk": self.author.pk, "related_field": "bio"}
434434
)
435-
resp = self.client.get(url)
435+
resp = self.client.get(url, data={"include": "metadata"})
436436
expected = {
437437
"data": {
438438
"type": "authorBios",
@@ -447,7 +447,14 @@ def test_retrieve_related_single_reverse_lookup(self):
447447
},
448448
},
449449
"attributes": {"body": str(self.author.bio.body)},
450-
}
450+
},
451+
"included": [
452+
{
453+
"attributes": {"body": str(self.author.bio.metadata.body)},
454+
"id": str(self.author.bio.metadata.id),
455+
"type": "authorBioMetadata",
456+
}
457+
],
451458
}
452459
self.assertEqual(resp.status_code, 200)
453460
self.assertEqual(resp.json(), expected)

‎rest_framework_json_api/renderers.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,10 @@ def _get_included_serializers(cls, serializer, prefix="", already_seen=None):
708708

709709
def get_includes_form(self, view):
710710
try:
711-
serializer_class = view.get_serializer_class()
711+
if "related_field" in view.kwargs:
712+
serializer_class = view.get_related_serializer_class()
713+
else:
714+
serializer_class = view.get_serializer_class()
712715
except AttributeError:
713716
return
714717

‎rest_framework_json_api/serializers.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ def validate_path(serializer_class, field_path, path):
140140
included_resources = get_included_resources(request)
141141
for included_field_name in included_resources:
142142
included_field_path = included_field_name.split(".")
143-
this_serializer_class = view.get_serializer_class()
143+
if "related_field" in view.kwargs:
144+
this_serializer_class = view.get_related_serializer_class()
145+
else:
146+
this_serializer_class = view.get_serializer_class()
144147
# lets validate the current path
145148
validate_path(
146149
this_serializer_class, included_field_path, included_field_name

0 commit comments

Comments
(0)

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