Repository: django-json-api/django-rest-framework-json-api

Issue:
SparseFieldsetsMixin does not use the correct configured JSON_API_FORMAT_FIELD_NAMES
## Description of the Bug Report
`SparseFieldsetsMixin` does not adjust field_names by configured format.

## Fix:


```python
class SparseFieldsetsMixin:
    """
    A serializer mixin that adds support for sparse fieldsets through `fields` query parameter.

    Specification: https://jsonapi.org/format/#fetching-sparse-fieldsets
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        context = kwargs.get("context")
        request = context.get("request") if context else None

        if request:
            sparse_fieldset_query_param = "fields[{}]".format(
                get_resource_type_from_serializer(self)
            )
            try:
                param_name = next(
                    key
                    for key in request.query_params
                    if sparse_fieldset_query_param == key
                )
            except StopIteration:
                pass
            else:
                fieldset = request.query_params.get(param_name).split(",")
                # iterate over a *copy* of self.fields' underlying OrderedDict, because we may
                # modify the original during the iteration.
                # self.fields is a `rest_framework.utils.serializer_helpers.BindingDict`
                for field_name, field in self.fields.fields.copy().items():
                    if (
                        field_name == api_settings.URL_FIELD_NAME
                    ):  # leave self link there
                        continue
                    # missing format_value()
                    correct_field_name = format_value(field_name, json_api_settings.FORMAT_FIELD_NAMES)
                    if correct_field_name not in fieldset:
                        self.fields.pop(field_name)
```


## Checklist

- [x] Certain that this is a bug (if unsure or you have a question use [discussions](https://github.com/django-json-api/django-rest-framework-json-api/discussions) instead)
- [x] Code snippet or unit test added to reproduce bug



Discussion:
I agree this is a bug. For compatibility as we do it in other spots it is better to run `undo_format_field_name` on the fieldset to solve this issue. PR is welcome.



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index eae89c70..9502f476 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
 
diff --git a/rest_framework_json_api/serializers.py b/rest_framework_json_api/serializers.py
index 75764a5d..26f6b02e 100644
--- a/rest_framework_json_api/serializers.py
+++ b/rest_framework_json_api/serializers.py
@@ -26,6 +26,7 @@
     get_resource_type_from_instance,
     get_resource_type_from_model,
     get_resource_type_from_serializer,
+    undo_format_field_name,
 )
 
 
@@ -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


Test patch:
diff --git a/tests/test_serializers.py b/tests/test_serializers.py
index 9d4200a3..98cb2850 100644
--- a/tests/test_serializers.py
+++ b/tests/test_serializers.py
@@ -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
@@ -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",
+    ]