Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drf-yasg swagger setup parameters IN_BODY is not worked

class Templates(APIView):
    parser_classes = (FormParser,)

    @swagger_auto_schema(manual_parameters=[
        openapi.Parameter('title', in_=openapi.IN_FORM, description='作品标题', type=openapi.TYPE_STRING, required=True)]
    )
    def post(self, reuqest):
        pass

when i specify in_=openapi.IN_FORM and parser_classes = (FormParser,) the swaager front page is worked, but if i modify in_=openapi.IN_BODY, there are some errors like below:

Traceback (most recent call last):
  File "C:\pythonenv\forecast_env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\pythonenv\forecast_env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\pythonenv\forecast_env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\pythonenv\forecast_env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\pythonenv\forecast_env\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\pythonenv\forecast_env\lib\site-packages\rest_framework\views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "C:\pythonenv\forecast_env\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\pythonenv\forecast_env\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
    raise exc
  File "C:\pythonenv\forecast_env\lib\site-packages\rest_framework\views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\views.py", line 94, in get
    schema = generator.get_schema(request, self.public)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\generators.py", line 254, in get_schema
    paths, prefix = self.get_paths(endpoints, components, request, public)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\generators.py", line 412, in get_paths
    operation = self.get_operation(view, path, prefix, method, components, request)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\generators.py", line 454, in get_operation
    operation = view_inspector.get_operation(operation_keys)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\inspectors\view.py", line 36, in get_operation
    parameters = self.add_manual_parameters(parameters)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\inspectors\view.py", line 162, in add_manual_parameters
    raise SwaggerGenerationError("specify the body parameter as a Schema or Serializer in request_body")
drf_yasg.errors.SwaggerGenerationError: specify the body parameter as a Schema or Serializer in request_body
[16/Jul/2020 16:39:48] "GET /api/swagger?format=openapi HTTP/1.1" 500 19229

is there anyone can give me a example about setup application/json parameters on drf-yasg?

drf-yasg  1.17.1
Django    3.0.5
like image 568
Randon Peng Avatar asked Jul 16 '20 08:07

Randon Peng


1 Answers

You should use request_body parameter

    @swagger_auto_schema(methods=['post'],
                         request_body=openapi.Schema(
                             type=openapi.TYPE_OBJECT,
                             required=['version'],
                             properties={
                                 'version': openapi.Schema(type=openapi.TYPE_STRING)
                             },
                         ),
                         operation_description='Uninstall a version of Site')
    def post(self, request):
        pass
like image 144
Kolya Terletskyi Avatar answered Oct 19 '22 12:10

Kolya Terletskyi