Override type name in the drf-spectacular - generated OpenAPI schema

202 views Asked by At

I'm stuck with what seems like it should be a reasonably simple problem.

I have DRF application, and I use drf-spectacular to generate OpenAPI schema. In my app, I have a class, let's call it Foo, which is fairly deep in the hierarchy of a serialized response. The problem is that for historical reasons I need to keep calling it Foo in the code but in the schema I need to refer to it as Bar.

There don't appear to be any explicit ways (e.g. decorators) for that, and I haven't figured out how do "override the name" with the general-purpose pre- or post-process hooks. Any suggestions will be appreciated!

1

There are 1 answers

0
tesSb99 On

It would be more helpful to include an example of your code, from what I understand you're trying to override a serialiser's name that is used in a hierarchy of a serialized response, to do that you can decorate your Foo serializer with @extend_schema_serializer. Your code will look like this:

    @extend_schema_serializer(component_name="Bar")
    class Foo(serialisers.Serializer): 
        field = .. 
        
    class Foo1(serializers.Serializer):
        foos = Foo(many=True)