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

duplicate choices in FilterSet.ChoiceFilter #991

Closed
allynt opened this issue May 26, 2023 · 5 comments
Closed

duplicate choices in FilterSet.ChoiceFilter #991

allynt opened this issue May 26, 2023 · 5 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@allynt
Copy link

allynt commented May 26, 2023

Describe the bug

I have a ViewSet w/ a Filterset which includes a ChoiceFilter. When the schema is rendered in the browser, it duplicates each choice. (See the attached image to understand what I mean).

To Reproduce

from django.db import models
from django_filters import rest_framework as filters
from rest_framework import serializers, viewsets

class ThingType(models.TextChoices):
    ONE = "one", "One"
    TWO = "two", "Two"
    THREE = "three", "Three"


class Thing(models.Model):
    name = models.CharField(max_length=64)
    type = models.CharField(max_length=64, choices=ThingType.choices)

class ThingSerializer(serializers.ModelSerializer):
    class Meta:
        model = Thing
        fields = (
            "name",
            "type",
        )

class ThingFilterSet(filters.FilterSet):
    class Meta:
        model = Thing
        fields = {
            "type",
        }

    type = filters.ChoiceFilter(choices=ThingType.choices)


class ThingViewSet(viewsets.ModelViewSet):
    queryset = Thing.objects.all()
    serializer_class = ThingSerializer
    lookup_field = "name"

    filter_backends = (filters.DjangoFilterBackend, )
    filterset_class = ThingFilterSet

Expected behavior
There should only be one choice rendered in the schema documentation.
thing_api

@allynt
Copy link
Author

allynt commented May 29, 2023

Aha. I think one of the choices is coming from default documentation. And the other one is actually rendering choices. This can be fixed be either adding explicit help_text on the filter field, adding explicit help_text on the model field, or adding explicit documentation on the view using something like:

@extend_schema_view(
    list=extend_schema(parameters=[
        OpenApiParameter(name='type', description="some help text")
    ])
)

@tfranzel
Copy link
Owner

tfranzel commented May 29, 2023

Hi,

these are not duplicated. Each choice usually has a value and a name/description. In your case this just looks superfluous. E.g. consider having integers as choices without a proper description, would make the schema not really useful. Other times, the choices are just abbreviations that make no sense without description. Therefore we add this where we think it is useful.
Providing customized help_text/description signals to us that you deal with this yourself and so we add nothing then.

You can turn off this added list with the setting: "ENUM_GENERATE_CHOICE_DESCRIPTION": False

@allynt
Copy link
Author

allynt commented May 29, 2023

I don't understand your answer. I don't mean that the value one is duplicated by the description One. I mean that the set of 3 choices: [("one", "One"), ("two", "Two"), ("three", "Three")] is repeated twice. So it looks like there are 6 choices according to the documentation.

@tfranzel
Copy link
Owner

ohh, I totally misunderstood your question. That of course looks like a bug and should not happen.

@tfranzel tfranzel added bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels May 29, 2023
tfranzel added a commit that referenced this issue May 29, 2023
@allynt
Copy link
Author

allynt commented Jun 16, 2023

Sorry for the long delay. That fixes the problem. This issue can be closed. Thanks very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants