I am using drf-yasg
for documentation,I am using redoc
and it's working great. But I want to add x-server
and x-taggroups
. I would like to set custom tags for views, and add them to their own taggroups, but I cannot find anything in the docs about this, how should I go about this?
3
votes
1 Answers
1
votes
You can include arbitrary extra data in your schema (such as x-server
and x-tagGroups
vendor extensions) by subclassing OpenAPISchemaGenerator
.
class MyOpenAPISchemaGenerator(OpenAPISchemaGenerator):
def get_schema(self, request=None, public=False):
# Get the Swagger object generated by the superclass
swagger = super().get_schema(request, public)
# Add any extra fields you want
swagger['x-tagGroups']=[{
'name': 'My Group 1',
'tags': ['myTag1', 'myTag2']
},{
'name': 'My Group 2',
'tags': ['myTag1', 'myTag2', 'myTag3']
}]
return swagger
Then, make sure you provide your custom generator class when getting a SchemaView
class:
schema_view = get_schema_view(
openapi.Info(
title="Your API",
default_version='v1',
description="Your description",
),
public=True,
generator_class=MyOpenAPISchemaGenerator, # <--- Don't forget this
permission_classes=[permissions.AllowAny],
)