From 38b953bb7e1b412a4a4bb258d9ad3ce0d3ebf239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20ROCHER?= Date: Thu, 28 Jul 2016 15:39:49 +0200 Subject: [PATCH] Export the docstring of the method in the Link description For viewset, it exports the corresponding action (list, retrieve, update, etc.). For simple APIView, it returns the http handler (get, post, put, etc.). --- rest_framework/schemas.py | 10 +++++++++- tests/test_schemas.py | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index 2fb9c7eb5..d249ac4e1 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -6,7 +6,7 @@ from django.core.urlresolvers import RegexURLPattern, RegexURLResolver from django.utils import six from django.utils.encoding import force_text -from rest_framework import exceptions, serializers +from rest_framework import exceptions, serializers, viewsets from rest_framework.compat import coreapi, uritemplate, urlparse from rest_framework.request import clone_request from rest_framework.views import APIView @@ -207,10 +207,18 @@ class SchemaGenerator(object): else: encoding = None + if isinstance(view, viewsets.GenericViewSet): + actions = getattr(callback, 'actions', self.default_mapping) + action = actions[method.lower()] + view_fn = getattr(callback.cls, action, None) + else: + view_fn = getattr(callback.cls, method.lower(), None) + return coreapi.Link( url=urlparse.urljoin(self.url, path), action=method.lower(), encoding=encoding, + description=view_fn.__doc__ if view_fn else '', fields=fields ) diff --git a/tests/test_schemas.py b/tests/test_schemas.py index a2c723a8b..f5b49abcd 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -38,6 +38,7 @@ class ExampleView(APIView): permission_classes = [permissions.IsAuthenticatedOrReadOnly] def get(self, request, *args, **kwargs): + """get documentation""" return Response() def post(self, request, *args, **kwargs): @@ -171,6 +172,7 @@ class TestSchemaGenerator(TestCase): 'read': coreapi.Link( url='/example-view/', action='get', + description='get documentation', fields=[] ) }