3

我正在使用drf-yasg文档,我正在使用redoc并且效果很好。但我想添加x-serverand x-taggroups。我想为视图设置自定义标签,并将它们添加到他们自己的标签组中,但是我在文档中找不到任何关于这个的东西,我应该怎么做?

4

1 回答 1

1

您可以通过子类化在架构中包含任意额外数据(例如x-serverx-tagGroups供应商扩展)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

然后,确保在获取类时提供自定义生成器SchemaView类:

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],
)
于 2020-11-22T12:48:58.047 回答