返回文章列表文章索引阅读全文
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 分钟

StarBlog博客开发笔记(26):集成Swagger接口文档

前言 这是StarBlog系列在2023年的第一篇更新😃~ 在之前的文章里,我们已经完成了部分接口的开发,接下来需要使用 curl、Postman 这类工具对这些接口进行测试,但接口一多,每次测试都要一个个填入地址和对应参数会比较麻烦… 我们需要一种直观的方式来汇总项目里的所有接口,并且如果能直接在里面调试接口,那就更好了。 Swagger:诶嘿,说的不就是我吗?😎 Swagger介绍 来一段

2023-03-11 · StarBlog博客开发笔记 · 约 1 分钟

操作系统学习二:进程同步与互斥之生产者-消费者问题--NetCore实现

目的 1. 掌握进程(线程)的同步与互斥。 2. 掌握生产者消费者问题的实现方法。 3. 掌握VC的多线程编程方法。 内容 本实验要求设计并实现一个进程,该进程拥有3个生产者线程和1个消费者线程,它们使用10个不同的缓冲区。需要使用如下信号量: 一个mutex信号量,用以阻止生产者线程和消费者线程同时操作缓冲区队列; 一个full信号量,当生产者线程生产出一个物品时可以用它向消费者线程发出信号;

2022-06-17 · 计科基础 · 约 1 分钟

讨论

共 0 条

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

加载评论中…