返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
0_Swagger与drf-yasg接口文档系列

Experience Summary: Customizing a Few Details of drf-yasg API Documentation

drf-yasg is an extension for Django REST Framework that can generate OpenAPI interfaces from routes. Using it, however, is quite different from Java platforms and .NET Core, and some customizations are rather cumbersome. Still, looking at the docs is fine enough, and you can get things working. This article records a few issues encountered in actual development. Although they are details, polishing these details can make things much more convenient for colleagues integrating with the APIs~ By the way, writing backends in Python is really fast—can you believe dozens of APIs in one morning

曦远 (DealiAxy)约 8 分钟
目录9 个章节

drf-yasg is an extension for Django REST Framework that can generate OpenAPI interfaces from routes. Using it, however, is quite different from Java platforms and .NET Core, and some customizations are rather cumbersome. Still, looking at the docs is fine enough, and you can get things working.

This article records a few issues encountered in actual development. Although they are details, polishing these details can make things much more convenient for colleagues integrating with the APIs~

By the way, writing backends in Python is really fast—can you believe dozens of APIs in one morning?

Setting Request Parameters and API Descriptions

Implementation Code

@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': '更新手机号成功'})

Result

References

Adding a Description to Each Group

drf-yasg does not support this feature by default. This is a solution I found while browsing Stack Overflow—a bit of a roundabout workaround...

The Default State Looks Like This

Code

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

Result After the Change

References

_EOF_

Experience Summary: Customizing a Few Details of drf-yasg API Documentation

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

相关文章

解决drf-yasg中的SwaggerAPI无法正确分组问题

swagger是后台开发中很好用的交互式文档,Django原本的Django-Swagger已经停止维护了,现在一般用drf_yasg这个包来实现文档,它里面支持swagger和redoc两种,redoc是静态的,作为导出文档的话不错,不过一般我们用swagger,因为可以在文档里面调试,非常方便。 Drf里面有个东西是AutoSchema,可以自动扫描ViewSet和APIView这类可以提供接

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

讨论

共 0 条

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

加载评论中…