曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
0_Swagger与drf-yasg接口文档系列

Solving the Issue of Swagger API Not Grouping Correctly in drf-yasg

Swagger is a very useful interactive documentation tool in backend development. Django's original Django-Swagger has stopped being maintained. Now people generally use the drf_yasg package to implement documentation. It supports both swagger and redoc. redoc is static, which is nice for exporting documentation, but we generally use swagger because you can debug inside the documentation, which is very convenient. DRF has something called AutoSchema that can automatically scan ViewSet and APIView and similar that can provide

曦远 (DealiAxy)约 6 分钟
目录5 个章节

Swagger is a very useful interactive documentation tool in backend development. Django's original Django-Swagger has stopped being maintained. Now people generally use the drf_yasg package to implement documentation. It supports both swagger and redoc. redoc is static, which is nice for exporting documentation, but we generally use swagger because you can debug inside the documentation, which is very convenient.

DRF has something called AutoSchema that can automatically scan places that provide APIs such as ViewSet and APIView. Unlike Spring's annotation-based documentation definition, in DRF you generally don't need to manually configure the name and description of each endpoint; just write them in the pydoc. However, this AutoSchema isn't very accurate either. It groups by URL, and especially with multi-level URLs like ours, it only groups by the first-level URL, so you get the ungrouped effect shown below.

Ungrouped Effect

Effect After Grouping

Solution

Define a class that inherits from SwaggerAutoSchema to customize the tags configuration, so we decide which level of the URL to use as the grouping tag.

Create a file, in this example: config/swagger.py

from drf_yasg.inspectors import SwaggerAutoSchema

class CustomSwaggerAutoSchema(SwaggerAutoSchema):
    def get_tags(self, operation_keys=None):
        tags = super().get_tags(operation_keys)

        if "v1" in tags and operation_keys:
            #  `operation_keys` 内容像这样 ['v1', 'prize_join_log', 'create']
            tags[0] = operation_keys[1]

        return tags

Then configure it in settings:

# Swagger 配置
SWAGGER_SETTINGS = {
    'DEFAULT_AUTO_SCHEMA_CLASS': 'config.swagger.CustomSwaggerAutoSchema',
}

That's all it takes~

References

Welcome to Discuss

I've compiled a series of technical articles and materials. Reply with linux, flutter, c#, netcore, android, kotlin, java, python, etc. in the backend of the WeChat official account 「程序设计实验室」 to get related technical articles and materials. If you have any questions, you can also leave a message in the official account backend~

_EOF_

Solving the Issue of Swagger API Not Grouping Correctly in drf-yasg

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

经验总结-drf-yasg接口文档几个细节的自定义方式

drf-yasg是Django RestFramework的一个扩展, 可以根据路由生成OpenApi接口, 不过用起来和在Java平台和.NetCore有很多不一样的, 有些自定义的地方也比较麻烦, 不过看看文档也还好, 可以解决. 本文记录几个在实际开发中遇到的, 虽然是细节, 但是优化好细节可以给对接的同事带来很大的方便~ 顺带一提, Python写后台真的太快了, 一上午出几十个接口你能信

2022-04-04 · 0_Swagger与drf-yasg接口文档系列 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…