djangodjango-rest-frameworkopenapidrf-spectacular

Is there a way to change the autogenerated shema name of a serializer in drf-spectacular


I got many serializers named 'InputSerializer' and 'OutputSerializer' which translates to 'Input' and 'Output' schema name in drf-spectacular. This ends up referring the api endpoints to the same schema. Is there a way to override the autogenerated schema names of these serializers without changing the name of the class?


Solution

  • I've run into this a bunch, but never tried to solve it. Looking at the docs I found extended_schema_serializer, which might do what you need. Here is the full api, and the relevant point:

    @extended_schema_serializer(component_name="SomeNiceReallyLongId")
    class Input(Serializer):
        # pass
    
    

    Its kinda long and ugly, but that can be fixed by a decorator on the decorator :D

    Edit:

    I ended up implementing this. Here is the small wrapper I wrote. It just makes things shorter and consistent between serializers and fields.

    You can use extended_schema_serializer more than once on a serializer, so this won't break anything.

    @oapi.name("UserEditRequest")
    @extend_schema_serializer(examples=[]) # other settings
    class EditSerializer(ModelSerializer):
        pass
    
    # oapi.py
    from drf_spectacular.utils import set_override as _set_override
    
    def name(val: str):
        def decorator(klass):
            if issubclass(klass, BaseSerializer):
                _set_override(klass, "component_name", val)
            elif isinstance(klass, Field):
                _set_override(klass, "field_component_name", val)
            else:
                raise Exception(f"Unhandled class: {klass}")
    
            return klass
    
        return decorator