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

Strange behaviour of enums with integer choices #1104

Closed
MiloLug opened this issue Nov 13, 2023 · 4 comments
Closed

Strange behaviour of enums with integer choices #1104

MiloLug opened this issue Nov 13, 2023 · 4 comments

Comments

@MiloLug
Copy link

MiloLug commented Nov 13, 2023

I know drf-spectacular optimizes enums so that among enums with the same choices only one will be left.
But in our project we use IntegerChoices and right now it makes the team to add literally every second enum to ENUM_NAME_OVERRIDES. This is a huge inconvenience in big projects.

Here is some example:

### models.py
from django.db import models
from django.utils.translation import gettext_lazy as _


class Availability(models.Model):
    class ScheduleType(models.IntegerChoices):
        WEEKLY = 0, _("Weekly")
        DAILY = 1, _("Daily")

    class DayTimeRangeType(models.IntegerChoices):
        FULL = 0, _("Full")
        PART = 1, _("Part")

    schedule_type = models.IntegerField(
        choices=ScheduleType.choices, default=ScheduleType.DAILY
    )
    time_range_type = models.IntegerField(
        choices=DayTimeRangeType.choices, default=DayTimeRangeType.PART
    )


### serializers.py
from rest_framework import serializers

from .models import Availability


class AvailabilityModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Availability
        fields = "__all__"

Maybe you should also check the labels of choices to determine, which choice sets are "similar"? Or at least you could make this the default behaviour when dealing with integer choices/enums, since they generally have similar choices.

@tfranzel
Copy link
Owner

somewhat related to #790

It is a problem that got elevated to a bug since we were adding labels to the description string. This of course can lead to wrong/confusing components. I'm aware and trying to come up with a fix for it soon.

@gabn88
Copy link

gabn88 commented Nov 20, 2023

@tfranzel Thank you for looking into this. This is still one of the main problems we are facing in an otherwise very helpful and great package!

@tfranzel
Copy link
Owner

It would be helpful if you guys could try out #1113

It introduces an Id into the schema for keeping track of the enum so the mix-up of similar enums does not happen anymore. Those Ids are then cleared afterwards via hook.

tfranzel added a commit that referenced this issue Nov 28, 2023
Fix Enum collision with same choices & varying labels #790 #1104
@gabn88
Copy link

gabn88 commented Nov 29, 2023

#1113 Fixes this error for us. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants