2

I want to generate swagger (OpenAPI) schema for my DRF project.

How can I add query parameters specification to the generated schema?

Take a look at FileViewSet.list method. Here request.GET parameter project should be documented (required), but automatic schema generation misses it.

Here is example api project:

# views.py

class FileSerializer(serializers.ModelSerializer):    
    class Meta:
        model = File
        fields = ('name', 'id', 'project')


class FileViewSet(viewsets.ModelViewSet):
    queryset = File.objects.all()
    serializer_class = FileSerializer

    def list(self, request):
        project = request.GET.get("project", None)

        qs = self.get_queryset().filter(project=project)


router = routers.DefaultRouter(trailing_slash=False)
router.register(r'^file$', FileViewSet)


# OpenAPI Schema generation

class SwaggerRenderer(renderers.BaseRenderer):
    media_type = 'application/openapi+json'
    format = 'swagger'

    def render(self, data, media_type=None, renderer_context=None):
        codec = OpenAPICodec()
        return codec.dump(data)

schema_view = get_schema_view(
    title='API',
    public=True,
    renderer_classes=[SwaggerRenderer],
    authentication_classes=[],
    permission_classes=[AllowAny],
    patterns=router.urls,
)

api_urls = router.urls + [url(r'^schema$', schema_view, name="schema")]

And then inluding api_urls in the urls.py:

# urls .py

from .views import api_urls

urlpatterns = [
    url(r'^v1/', include(api_urls, namespace="v1")),
]

There is DRF coreapi ManualScheme docs for manually documenting APIView to add custom fields, but there is no documentation for ViewSet's.

2 Answers 2

3

In the same way as ManualSchema works for APIView, there is AutoSchema which works with ViewSet. It allows to describe custom and override automatically generated path parameters like id.

For example:

import coreapi
import coreschema
from rest_framework.schemas import AutoSchema

...

class YourViewSet(..., ViewSet):

    schema = AutoSchema(
        manual_fields=[
            coreapi.Field(
                'id',
                required=True,
                location='path',
                description='A unique integer value identifying specific your-model-name.',
                schema=coreschema.Integer(),
                ),
        ]
    )

This will add (or in this case override) path parameter id to schema and generate validation in swagger. enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

I think you have configured it a bit wrong. What I did in my project was to import get_swagger_view from rest_framework_swagger.views and added the view to the project urls and it worked like a charm.

Example:

# project/urls.py

from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='My API')

urlpatterns = [
    # other urls...
    url(r'^schema/$', schema_view),
]

Ref: Swagger Docs

Note: get_swagger_view is a shortcut to get the basic configurations. See advanced usage section to get more control.

2 Comments

And what about configuring query parameters? In this way, I will get only basic config.
Look at this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.