tfranzel/drf-spectacular

Different enums for the same field name resolution

Closed this issue · 2 comments

Describe the bug
I have a choice field which is called assign_to in multiple serializers. This field has different choices in different serializers depending on logic. However, the drf-spectacular throws the warning.
Warning: enum naming encountered a non-optimally resolvable collision for fields named "assign_to". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "AssignToDb2Enum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.
Warning: enum naming encountered a non-optimally resolvable collision for fields named "assign_to". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "AssignTo06dEnum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.

To Reproduce
The code related two both serializers:

class FirstSerializer(Serializer):
    assign_to = serializers.ChoiceField(
        label=_("Assign to"),
        choices=FirstChoiceClass.choices,
        required=True
    )
    
class SecondSerializer(Serializer):
    assign_to = serializers.ChoiceField(
        label=_("Assign to"),
        choices=SecondChoiceClass.choices,
        required=True
    )
image image

Expected behavior
I would like to resolve the enums properly. I have already tried something like:

    'ENUM_NAME_OVERRIDES': {
        'AssignToDb2Enum': 'some.path.to.first.class',
        'AssignTo06dEnum': 'some.path.to.second.class'
    }

Despite the fact that errors are gone it still generates the same documentation where instead of meaningful enums name I have AssignToDb2Enum and AssignTo06dEnum. So is there any way to resolve it and give the proper name to enums? Like AssignToFirstEnum and AssignToSecondEnum?

Oh... the override names are there for you choice.

    'ENUM_NAME_OVERRIDES': {
        'YourCategoryNameEnum': 'path.to.FirstChoiceClass',
        'YourOtherCategoryNameEnum': 'path.to.SecondChoiceClass'
    }

The AssignToDb2Enum was our name resolution. The override is there to give your custom names. The warning goes away if an override was found for that choice.

Thank you, it's exactly what I wanted to achieve