Skip to content

Avoided error when using include query parameter on related urls #914

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 2 commits into from
Apr 23, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ any parts of the framework not mentioned in the documentation should generally b
* 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.
* Deprecated `format_type` argument of `rest_framework_json_api.utils.format_link_segment`. Use `format_value` instead.

### Fixed

* Avoided error when using `include` query parameter on related urls (a regression since 4.1.0)

## [4.1.0] - 2021-03-08

### Added
Expand Down
11 changes: 11 additions & 0 deletions example/tests/integration/test_browsable_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ def test_browsable_api_with_included_serializers(single_entry, client):
)


def test_browsable_api_on_related_url(author, client):
url = reverse("author-related", kwargs={"pk": author.pk, "related_field": "bio"})
response = client.get(url, data={"format": "api"})
content = str(response.content)
assert response.status_code == 200
assert re.search(r"JSON:API includes", content)
assert re.search(
r'<input type="checkbox" name="includes" [^>]* value="metadata"', content
)


def test_browsable_api_with_no_included_serializers(client):
response = client.get(reverse("projecttype-list", kwargs={"format": "api"}))
content = str(response.content)
Expand Down
11 changes: 9 additions & 2 deletions example/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def test_retrieve_related_single_reverse_lookup(self):
url = reverse(
"author-related", kwargs={"pk": self.author.pk, "related_field": "bio"}
)
resp = self.client.get(url)
resp = self.client.get(url, data={"include": "metadata"})
expected = {
"data": {
"type": "authorBios",
Expand All @@ -447,7 +447,14 @@ def test_retrieve_related_single_reverse_lookup(self):
},
},
"attributes": {"body": str(self.author.bio.body)},
}
},
"included": [
{
"attributes": {"body": str(self.author.bio.metadata.body)},
"id": str(self.author.bio.metadata.id),
"type": "authorBioMetadata",
}
],
}
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.json(), expected)
Expand Down
5 changes: 4 additions & 1 deletion rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,10 @@ def _get_included_serializers(cls, serializer, prefix="", already_seen=None):

def get_includes_form(self, view):
try:
serializer_class = view.get_serializer_class()
if "related_field" in view.kwargs:
serializer_class = view.get_related_serializer_class()
else:
serializer_class = view.get_serializer_class()
except AttributeError:
return

Expand Down
5 changes: 4 additions & 1 deletion rest_framework_json_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ def validate_path(serializer_class, field_path, path):
included_resources = get_included_resources(request)
for included_field_name in included_resources:
included_field_path = included_field_name.split(".")
this_serializer_class = view.get_serializer_class()
if "related_field" in view.kwargs:
this_serializer_class = view.get_related_serializer_class()
else:
this_serializer_class = view.get_serializer_class()
# lets validate the current path
validate_path(
this_serializer_class, included_field_path, included_field_name
Expand Down