Skip to content

Commit

Permalink
Ensured that no fields are returned when sparse fields is set to an e…
Browse files Browse the repository at this point in the history
…mpty value (#1240)
  • Loading branch information
sliverc authored Jun 28, 2024
1 parent 53469f3 commit d1163ce
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
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

0 comments on commit d1163ce

Please sign in to comment.