django - 如何在 DRF-YASG 中为 DRF 和 DJANGO 消除 swagger-ui 中的 id 路径参数
问题描述
我正在使用 DRF-YASG 在 Swagger 中记录 API,并希望自定义/消除参数中显示的某些字段
我正在使用 Django 2.1.7、DRF 3.9.2 和 DRF-YASG 1.14.0 运行该项目。
所以,我想消除 swagger-ui 中显示的 ID(如“字符串”和“路径”),因为我通过 Schema 在正文请求中拥有它,但 swagger-ui 显示 id(自动生成的字段)在参数中。
在下面的屏幕中,您可以看到问题:
https://user-images.githubusercontent.com/5421182/54859641-70359d00-4cee-11e9-9b12-79ab57d12495.png
在这里,我的代码...
request_category_put = openapi.Schema(type=openapi.TYPE_OBJECT, required=['id','name'],
properties={
'id': openapi.Schema(type=openapi.TYPE_INTEGER,
title='Id', readOnly=True,
description='Id of the category'), ### <-- I have the ID here.
'name': openapi.Schema(type=openapi.TYPE_STRING,
title='Category', maxLength=200, minLength=1,
description='Name of the category')
},
example={
'id' : 1,
'name' : 'Business',
}
)
class CategoryDetail(APIView):
permission_classes = (IsAuthenticatedOrReadOnly,)
@swagger_auto_schema(
manual_parameters=[authorization],
request_body=request_category_put,
responses = {
'200' : response_category,
'400': 'Bad Request',
'404': 'Not found'
},
security=[security_endpoint],
operation_id='Update category',
operation_description='Update a specific category.',
)
def put(self, request, pk, format=None):
category = get_object_or_404(Category, pk=pk)
serializer = CategorySerializer(category, data=request.data)
if serializer.is_valid():
serializer.save(modified_by=self.request.user)
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
当我在 in 中添加字段时manual_parameters
,@swagger_auto_schema
仅更改 this 的属性...但该字段仍然存在。
解决方案
查看如何manual_parameters
实现的代码,它看起来不可能。看:
drf_yasg.inspectors.view.get_operation()
def get_operation(self, operation_keys=None):
operation_keys = operation_keys or self.operation_keys
consumes = self.get_consumes()
produces = self.get_produces()
body = self.get_request_body_parameters(consumes)
query = self.get_query_parameters()
parameters = body + query
parameters = filter_none(parameters)
parameters = self.add_manual_parameters(parameters)
在该函数中,有一个调用add_manual_parameters()
仅将您指定的覆盖添加到现有参数列表中。因此,您必须添加一个选项来替换现有参数或添加一个新的 manual_overrides_remove 来删除特定参数。我建议在drf_yasg github页面中提出问题或提交 PR 以添加此功能。
推荐阅读
- php - 在没有开关的情况下拆分函数的处理和响应(寻找替代方案)
- sapui5 - 如何使简单的表单响应?
- c# - 调试期间的 CsvHelper 错误 - System.Linq.SystemCore_EnumerableDebugView
- android - 使用 Gson 反序列化,但 App 已更新(新对象结构)
- protractor - 我想在量角器黄瓜框架中并行运行 5 个 chrome 会话上的一个功能文件
- react-native - 无法在android中添加联系人
- linux - 运行 Gtlab CI 作业以使用 webdriverio 执行自动化测试时出现“加载共享库时出错:libnss3.so”
- mysql - MySQL查找每个月的员工人数
- javascript - 如何使用结构指令?
- c# - 用更少的代码行做事有性能优势吗?