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

Not allowed to use word "Request" as a part of serializer name for some reason #1286

Open
KristobalJunta opened this issue Sep 4, 2024 · 0 comments

Comments

@KristobalJunta
Copy link

Describe the bug
I was extending my API and creating new serializers. After adding another one and regenerating the schema I encountered this error message:

Warning [FilledFormAnswerViewSet > FilledFormAnswerSerializer > FormQuestionSerializer]: Encountered 2 components with identical names "FormQuestionRequest" and different classes <class 'forms.serializers.FormQuestionSerializer'> and <class 'forms.serializers.FormQuestionRequestSerializer'>. This will very likely result in an incorrect schema. Try renaming one.

This error appears after I type-hint a SerializerMethodField.
Class structure is as follows:

class FormQuestionRequestMappingSerializer(serializers.Serializer):
    label = serializers.CharField()
    value = serializers.CharField()

    class Meta:
        fields = ("label", "value")


class FormQuestionRequestSerializer(serializers.Serializer):
    url = serializers.CharField()
    mapping = serializers.DictField(
        child=FormQuestionRequestMappingSerializer(),
        allow_empty=False,
    )
    params = serializers.DictField()

    class Meta:
        fields = ("url", "mapping", "params")

class FormQuestionSerializer(serializers.ModelSerializer):
    choices = FormQuestionChoiceSerializer(many=True, read_only=True)
    request_params = serializers.SerializerMethodField()

    @extend_schema_field(FormQuestionRequestSerializer)
    def get_request_params(self, obj):
        if obj.data_type != QuestionDataType.REQUEST:
            return None

        return dict(
            url=reverse(obj.request_field_route) if obj.request_field_route else None,
            mapping={
                "value": obj.request_field_mapping.get("value", "id"),
                "label": obj.request_field_mapping.get("label", "title"),
            },
            request_params=obj.request_field_params,
        )

    class Meta:
        model = FormQuestion
        fields = (
            "id",
            "title",
            "data_type",
            "request_params",
        )

And as soon as I rename FormQuestionRequestSerializer to NOT contain the exact "Request" word - the error is gone.
This serializer class name is unique in the project - so I am sure it does not clash with an existing one.

I also have a model class FormQuestion in my app, and maybe somehow that causes the name conflict, but I don't quite get how it may happen.

DRF-spectacular settings are as follows:

SPECTACULAR_SETTINGS = {
    "TITLE": "API Project",
    "DESCRIPTION": "API for my project",
    "VERSION": "1.0.0",
    "COMPONENT_SPLIT_REQUEST": True,
    "POSTPROCESSING_HOOKS": [],
}

Expected behavior
Being able to name serializer classes without causing critical errors.

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

1 participant