Search code examples
djangoswaggerpython-3.6openapidrf-yasg

cannot access the passed file from swagger ui inside django debugger(pdb)


created an api and added swagger to the api with the help of the package

drf-yasg

the current updated version 1.20.0, then added code like this

success_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='200'), 'success': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})
    
error_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='400'), 'error': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})

class TestView(APIView):
    api_view = ['POST']
    authentication_classes = [SessionAuthentication, TokenAuthentication]

    invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)

    @swagger_auto_schema(
        manual_parameters=[invitation_file], operation_description="description",
        responses={200: success_res_data, 400: error_res_data}
    )
    def post(self, request):
        invitation_file = request.data.get('invitation_file', None)

    invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)

    @swagger_auto_schema(
        manual_parameters=[invitation_file], operation_description="description",
        responses={200: success_res_data, 400: error_res_data}
    )
    def post(self, request):
        invitation_file = request.data.get('invitation_file', None)

this invitation_file variable is returning None even if we pass the file from front-end


Solution

  • after a little research and checking the same api in postman, changed the code from whats above to

    success_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='200'), 'success': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})
        
    error_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='400'), 'error': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})
    
    class TestView(APIView):
        api_view = ['POST']
        authentication_classes = [SessionAuthentication, TokenAuthentication]
    
        invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)
    
    
        @swagger_auto_schema(
                manual_parameters=[invitation_file],operation_description="API Description", consumes="multipart/form-data",
            responses={200: success_res_data, 400: error_res_data}
        )
        def post(self, request):
            invitation_file = request.data.get('invitation_file', None)
    
        invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)
    
        @swagger_auto_schema(
            manual_parameters=[invitation_file], operation_description="description",
            responses={200: success_res_data, 400: error_res_data}
        )
        def post(self, request):
            invitation_file = request.data.get('invitation_file', None)
    

    now click on the unlock button on the right of the api and add the "Token auth-token" and click authenticate

    now after calling the api and using pdb the value of the passed file is shown for the variable invitation_file