Skip to content

Stopped SparseFieldsetsMixin interpreting invalid fields parameter #842

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
Oct 16, 2020
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ This release is not backwards compatible. For easy migration best upgrade first
* Removed obsolete `source` argument of `SerializerMethodResourceRelatedField`
* Removed obsolete setting `JSON_API_SERIALIZE_NESTED_SERIALIZERS_AS_ATTRIBUTE` to render nested serializers as relationships. Default is as attribute now.

### Fixed

* Stopped `SparseFieldsetsMixin` interpretting invalid fields query parameter (e.g. invalidfields[entries]=blog,headline)

## [3.2.0] - 2020-08-26

Expand Down
34 changes: 29 additions & 5 deletions example/tests/integration/test_sparse_fieldsets.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
import pytest
from django.urls import reverse
from rest_framework import status

pytestmark = pytest.mark.django_db


def test_sparse_fieldset_ordered_dict_error(multiple_entries, client):
def test_sparse_fieldset_valid_fields(client, entry):
base_url = reverse('entry-list')
querystring = '?fields[entries]=blog,headline'
# RuntimeError: OrderedDict mutated during iteration
response = client.get(base_url + querystring)
assert response.status_code == 200 # succeed if we didn't fail due to the above RuntimeError
response = client.get(base_url, data={'fields[entries]': 'blog,headline'})
assert response.status_code == status.HTTP_200_OK
data = response.json()['data']

assert len(data) == 1
entry = data[0]
assert entry['attributes'].keys() == {'headline'}
assert entry['relationships'].keys() == {'blog'}


@pytest.mark.parametrize("fields_param", ['invalidfields[entries]', 'fieldsinvalid[entries'])
def test_sparse_fieldset_invalid_fields_parameter(client, entry, fields_param):
"""
Test that invalid fields query parameter is not processed by sparse fieldset.

rest_framework_json_api.filters.QueryParameterValidationFilter takes care of error
handling in such a case.
"""
base_url = reverse('entry-list')
response = client.get(base_url, data={'invalidfields[entries]': 'blog,headline'})
assert response.status_code == status.HTTP_200_OK
data = response.json()['data']

assert len(data) == 1
entry = data[0]
assert entry['attributes'].keys() != {'headline'}
assert entry['relationships'].keys() != {'blog'}
2 changes: 1 addition & 1 deletion rest_framework_json_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, *args, **kwargs):
)
try:
param_name = next(
key for key in request.query_params if sparse_fieldset_query_param in key
key for key in request.query_params if sparse_fieldset_query_param == key
)
except StopIteration:
pass
Expand Down