【问题标题】:How to eliminate id path parameter in swagger-ui in DRF-YASG for DRF and DJANGO如何在 DRF-YASG 中为 DRF 和 DJANGO 消除 swagger-ui 中的 id 路径参数
【发布时间】:2019-08-14 09:21:18
【问题描述】:

我正在使用 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)

当我在@swagger_auto_schema 中添加manual_parameters 中的字段时,只需更改此的属性...但该字段仍然存在。

【问题讨论】:

    标签: django django-rest-framework drf-yasg


    【解决方案1】:

    查看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 以添加此功能。

    【讨论】:

      【解决方案2】:

      尝试在 serrializer Meta-field read_only_fields 中添加要从 swagger 请求描述中排除的参数名称

      喜欢

      class SomeSerializer(ModelSerializer):
          ...
          class Meta:
              ...
              read_only_fields = ["id", ...]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-05
        • 2020-07-26
        • 1970-01-01
        • 2021-07-27
        • 2018-07-19
        • 2021-11-09
        • 2022-12-21
        • 1970-01-01
        相关资源
        最近更新 更多