springboot自定义404_经验总结: drfyasg接口文档几个细节的自定义方式

792523d31391210c4fabb442e1e6fd39.png

drf-yasg是Django RestFramework的一个扩展, 可以根据路由生成OpenApi接口, 不过用起来和在Java平台和.NetCore有很多不一样的, 有些自定义的地方也比较麻烦, 不过看看文档也还好, 可以解决.

本文记录几个在实际开发中遇到的, 虽然是细节, 但是优化好细节可以给对接的同事带来很大的方便~

顺带一提, Python写后台真的太快了, 一上午出几十个接口你能信?

设置请求参数和接口说明

实现代码

@swagger_auto_schema(request_body=openapi.Schema(
    type=openapi.TYPE_OBJECT,
    required=['phone'],
    properties={'phone': openapi.Schema(type=openapi.TYPE_STRING)}
), operation_summary='更新手机号码')
@action(detail=True, methods=['PUT'], permission_classes=[permissions.IsAuthenticated])
def phone(self, request, pk=None):
    """更新手机号码"""
    if request.method == 'POST':
        phone_num = request.data.get('phone')
        profile_obj: UserProfile = get_object_or_404(UserProfile.objects.all(),
                                                     user=request.user)
        profile_obj.phone = phone_num
        profile_obj.save()
        return Response({'detail': '更新手机号成功'})

效果

7fdb9f1313a1d0603e017ae849fa8add.png

参考资料

  • 官网文档 https://drf-yasg.readthedocs.io/en/stable/_modules/drf_yasg/inspectors/query.html?highlight=openapi.Parameter
  • Github issues https://github.com/axnsan12/drf-yasg/issues/280

给每个分组加上说明

drf-yasg默认是不支持这个功能的, 这是我在翻Stack Overflow时找到的解决方案, 算是有点曲线救国吧...

默认状态是这样的

c3ca3a7736d704d2cd60b2c2dde89f94.png

代码

class CustomOpenAPISchemaGenerator(OpenAPISchemaGenerator):
    """重写 OpenAPISchemaGenerator 实现每个tag的说明文本"""

    def get_schema(self, request=None, public=False):
        swagger = super().get_schema(request, public)

        swagger.tags = [
            {
                "name": "core",
                "description": "核心功能",
            },
            {
                'name': 'external_units',
                'description': '外部单位'
            },
            {
                'name': 'field_investigation',
                'description': '现场勘查'
            },
            {
                'name': 'require',
                'description': '需求'
            },
            {
                'name': 'require_delivered',
                'description': '需求分配'
            },
            {
                'name': 'scheme_appraisal',
                'description': '方案评审'
            }
        ]

        return swagger

修改完的效果

7f19cc8d8c855fb4bf54430cae2dbf85.png

参考资料

  • https://stackoverflow.com/questions/62572389/django-drf-yasg-how-to-add-description-to-tags
4f42c033aa8e2e4f03a3a319d088ba63.png推荐阅读 使用Kotlin搭配Springboot开发RESTFul接口(二)自定义配置、跨域、分页 聊聊Django应用的部署和性能的那些事儿 数据分析必备技能!3分钟教会你如何在PyQt中画好看的图表 Flutter移动应用快速构建实践——状态管理、国际化、数据持久化、性能优化(二) 无需折腾的Linux发行版:Manjaro体验与配置笔记,开箱即用 真香! Asp.Net Core学习笔记:(二)视图、模型、持久化、文件、错误处理、日志 1bf3d45cf45774aca048b48e46516552.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值