Skip to content

Ensured that sparse fieldset support formatted field names #1286

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
Jul 18, 2025
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 @@ -19,6 +19,7 @@ any parts of the framework not mentioned in the documentation should generally b

* Ensured that interpreting `include` query parameter is done in internal Python naming.
This adds full support for using multipart field names for includes while configuring `JSON_API_FORMAT_FIELD_NAMES`.
* Ensured that sparse fieldset fully supports `JSON_API_FORMAT_FIELD_NAMES`.

### Removed

Expand Down
6 changes: 5 additions & 1 deletion rest_framework_json_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get_resource_type_from_instance,
get_resource_type_from_model,
get_resource_type_from_serializer,
undo_format_field_name,
)


Expand Down Expand Up @@ -89,7 +90,10 @@ def _readable_fields(self):
sparse_fieldset_query_param
)
if sparse_fieldset_value is not None:
sparse_fields = sparse_fieldset_value.split(",")
sparse_fields = [
undo_format_field_name(sparse_field)
for sparse_field in sparse_fieldset_value.split(",")
]
return (
field
for field in readable_fields
Expand Down
20 changes: 20 additions & 0 deletions tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from django.db import models
from rest_framework.request import Request
from rest_framework.utils import model_meta

from rest_framework_json_api import serializers
Expand Down Expand Up @@ -84,3 +85,22 @@ class Meta:
"verified",
"uuid",
]


def test_readable_fields_with_sparse_fields(client, rf, settings):
class TestSerializer(serializers.Serializer):
name = serializers.CharField()
value = serializers.CharField()
multi_part_name = serializers.CharField()

class Meta:
resource_name = "test"

settings.JSON_API_FORMAT_FIELD_NAMES = "camelize"
request = Request(rf.get("/test/", {"fields[test]": "value,multiPartName"}))
context = {"request": request}
serializer = TestSerializer(context=context)
assert [field.field_name for field in serializer._readable_fields] == [
"value",
"multi_part_name",
]