Skip to content

Ensured that no fields are returned when sparse fields is set to an empty value #1240

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
Jun 28, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ any parts of the framework not mentioned in the documentation should generally b
### Fixed

* Re-enabled overwriting of url field (regression since 7.0.0)
* Ensured that no fields are rendered when sparse fields is set to an empty value. (regression since 7.0.0)

## [7.0.1] - 2024-06-06

Expand Down
2 changes: 1 addition & 1 deletion rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def _filter_sparse_fields(cls, serializer, fields, resource_name):
sparse_fieldset_value = request.query_params.get(
sparse_fieldset_query_param
)
if sparse_fieldset_value:
if sparse_fieldset_value is not None:
sparse_fields = sparse_fieldset_value.split(",")
return {
field_name: field
Expand Down
2 changes: 1 addition & 1 deletion rest_framework_json_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _readable_fields(self):
sparse_fieldset_value = request.query_params.get(
sparse_fieldset_query_param
)
if sparse_fieldset_value:
if sparse_fieldset_value is not None:
sparse_fields = sparse_fieldset_value.split(",")
return (
field
Expand Down
13 changes: 13 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ def test_list_allow_overwiritng_url_with_sparse_fields(self, client, url_instanc
}
]

@pytest.mark.urls(__name__)
def test_list_with_sparse_fields_empty_value(self, client, model):
url = reverse("basic-model-list")
response = client.get(url, data={"fields[BasicModel]": ""})
assert response.status_code == status.HTTP_200_OK
data = response.json()["data"]
assert data == [
{
"type": "BasicModel",
"id": str(model.pk),
}
]

@pytest.mark.urls(__name__)
def test_retrieve(self, client, model):
url = reverse("basic-model-detail", kwargs={"pk": model.pk})
Expand Down
Loading