简体   繁体   English

如何在 DRF-YASG 中为 DRF 和 DJANGO 消除 swagger-ui 中的 id 路径参数

[英]How to eliminate id path parameter in swagger-ui in DRF-YASG for DRF and DJANGO

I'm using DRF-YASG to document API in Swagger and want to customizing/eliminating some fields shown in parameters我正在使用 DRF-YASG 在 Swagger 中记录 API,并希望自定义/消除参数中显示的某些字段

I'm running the project with Django 2.1.7, DRF 3.9.2 and DRF-YASG 1.14.0.我正在使用 Django 2.1.7、DRF 3.9.2 和 DRF-YASG 1.14.0 运行该项目。

So, I want to eliminate the ID which is shown in swagger-ui (like a 'string' and 'path'), cause I have it in the body request through Schema, but swagger-ui shows the id (automatically generated field) in parameters.因此,我想消除 swagger-ui 中显示的 ID(如“字符串”和“路径”),因为我通过 Schema 在正文请求中包含它,但 swagger-ui 显示了 id(自动生成的字段)在参数中。

In the screen below, you can see the problem:在下面的屏幕中,您可以看到问题:

https://user-images.githubusercontent.com/5421182/54859641-70359d00-4cee-11e9-9b12-79ab57d12495.png https://user-images.githubusercontent.com/5421182/54859641-70359d00-4cee-11e9-9b12-79ab57d12495.png

Here, my code...在这里,我的代码...

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)

When I add the field in manual_parameters in @swagger_auto_schema , only change the attributes of this... but the field still is there.当我添加在外地manual_parameters@swagger_auto_schema ,只有改变这一属性...但仍然领域有。

Looking at the code for how manual_parameters is implemented, it doesn't look possible.查看如何实现manual_parameters的代码,看起来不可能。 See: drf_yasg.inspectors.view.get_operation()请参阅: 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)

in that function there is a call to add_manual_parameters() which only adds the overrides you have specified to the existing list of params.在该函数中,有一个对add_manual_parameters()的调用,它只会将您指定的覆盖添加到现有的参数列表中。 So you'd have to add an option either to replace the existing params or add a new manual_overrides_remove which would remove specific parameters.因此,您必须添加一个选项来替换现有参数或添加一个新的 manual_overrides_remove 来删除特定参数。 I would suggest raising an Issue in the drf_yasg github pages or submit a PR to add this feature.我建议在drf_yasg github页面中提出问题或提交 PR 以添加此功能。

Try to add your parameter name that you want to exclude from swagger request description in serrializer Meta-field read_only_fields尝试在序列化器元字段read_only_fields添加要从 swagger 请求描述中排除的参数名称

Like as像作为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM