Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRF 3.6: How to document input parameters in APIView (for automatic doc generation)?

I'm struggling with DRF 3.6 auto-generated interactive documentation to provide input parameters to fill in interactive mode.

As a result, I get an empty windows for my POST request (which would require 3 parameters actually):

enter image description here

With Swagger, I could do it directly in docstring with some YAML. Now, after browsing DRF documentation, I can't find the way to do it.

class ActivateCustomerView(APIView):

    permission_classes = (AllowAny,)

    def post(self, request):
        """ View dedicated to activating a pre-recorded customer 
            # Should I add some parameters here?
        """

        serializer = ActivateCustomerSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        # ...
like image 328
David D. Avatar asked May 03 '17 22:05

David D.


People also ask

Does Swagger support Dynamic Response Framework (DRF)?

There is explicit support for swagger-codegen, SwaggerUI and Redoc , i18n, versioning, authentication, polymorphism (dynamic requests and responses), query/path/header parameters, documentation and more. Several popular plugins for DRF are supported out-of-the-box as well.

How do I add API documentation to my project?

To install the API documentation, you'll need to include it in your project's URLconf: /docs/ - The documentation page itself. /docs/schema.js - A JavaScript resource that exposes the API schema. Note: By default include_docs_urls configures the underlying SchemaView to generate public schemas.

What is included in the API documentation?

The built-in API documentation includes: Documentation of API endpoints. Automatically generated code samples for each of the available API client libraries. Support for API interaction. The coreapi library is required as a dependency for the API docs.

What is the use of the rest_Framework documentation module?

The rest_framework.documentation module provides three helper functions to help configure the interactive API documentation, include_docs_urls (usage shown above), get_docs_view and get_schemajs_view.


1 Answers

I Got the answer from Tom Christie:

serializer_class by itself isn't enough - the view needs to implement get_serializer, see: https://github.com/encode/django-rest-framework/blob/master/rest_framework/schemas.py#L570

So in My case, adding this works well:

def get_serializer(self):
    return ActivateCustomerSerializer()
like image 121
David D. Avatar answered Sep 28 '22 09:09

David D.