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
目录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 分钟
2022-五四青年节分享
很快啊,五一劳动节假期就到了最后时刻了,虽然这个假期的天气不是很好,感觉几天有点虚度了的感觉,不过今天是五四青年节,总该来写点什么。 不知道为啥,总觉得今年的五四青年节有点冷清,是心境的改变还是其他原因就不得而知了,在写这段话前,我想到了之前上了热搜的鲁迅语录: 愿中国青年都摆脱冷气,只是向上走,不必听自暴自弃者流的话。能做事的做事,能发声的发声。有一分热,发一分光,就令萤火一般,也可以在黑暗里发
2022-11-07 · 2022 · 约 1 分钟
ChatGLM-6B本地部署
下载项目代码 git clone https://github.com/THUDM/ChatGLM-6B.git 创建虚拟环境,建议使用 conda 管理 conda create -n chatglm python==3.8 安装依赖 conda activate chatglm pip install -r requirements.txt conda install cudatoolkit=
2026-05-09 · LLM · 约 1 分钟
2020年第一次用上Struts2框架做Web开发的体验
SliiyStruts2 又名 sb-struts2,因为struts真的太难用了(也许在很多年前是很好用的,但是现在看来,被其他框架秒成渣) 前言 想不到我还是得和这struts框架打交道啊,从一开始学web的时候就十分抵制这类古老,使用反人类的框架,不过为了帮女朋友做个学校的作业,还是得搞一下,然而,半小时就写好的业务代码,因为我不熟悉这个框架和Java的这套体系,调试了半天才成功run起来…
2022-06-17 · Java · 约 1 分钟
讨论
共 0 条加载评论中…