Skip to content
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

SparseFieldsetsMixin does not use the correct configured JSON_API_FORMAT_FIELD_NAMES #1053

Open
2 tasks done
jokiefer opened this issue Feb 15, 2022 · 1 comment
Open
2 tasks done
Labels
bug good first issue Good for newcomers

Comments

@jokiefer
Copy link
Contributor

Description of the Bug Report

SparseFieldsetsMixin does not adjust field_names by configured format.

Fix:

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

  • Certain that this is a bug (if unsure or you have a question use discussions instead)
  • Code snippet or unit test added to reproduce bug
@jokiefer jokiefer added the bug label Feb 15, 2022
@sliverc
Copy link
Member

sliverc commented Feb 15, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants