Add swagger_fake_view marker to help detect fake views in get_serializer

Cleaner fix for #154
openapi3
Cristi Vîjdea 2018-06-29 17:46:05 +03:00
parent e0aec3ff45
commit 696ec3a94a
3 changed files with 17 additions and 1 deletions

View File

@ -174,6 +174,15 @@ in a few ways:
:ref:`@swagger_auto_schema <custom-spec-swagger-auto-schema>`, to prevent ``get_serializer`` from being called on
the view
* :ref:`exclude your endpoint from introspection <custom-spec-excluding-endpoints>`
* use the ``swagger_fake_view`` marker to detect requests generated by ``drf-yasg``:
.. code-block:: python
def get_serializer_class(self):
if getattr(self, 'swagger_fake_view', False):
return TodoTreeSerializer
raise NotImplementedError("must not call this")
.. _SCRIPT_NAME: https://www.python.org/dev/peps/pep-0333/#environ-variables
.. _FORCE_SCRIPT_NAME: https://docs.djangoproject.com/en/2.0/ref/settings/#force-script-name

View File

@ -243,6 +243,8 @@ class OpenAPISchemaGenerator(object):
view_method = getattr(view, method, None)
if view_method is not None: # pragma: no cover
setattr(view_method.__func__, '_swagger_auto_schema', overrides)
setattr(view, 'swagger_fake_view', True)
return view
def get_endpoints(self, request):

View File

@ -33,7 +33,12 @@ class NestedTodoView(RetrieveAPIView):
class TodoTreeView(viewsets.ReadOnlyModelViewSet):
queryset = TodoTree.objects.all()
serializer_class = TodoTreeSerializer
def get_serializer_class(self):
if getattr(self, 'swagger_fake_view', False):
return TodoTreeSerializer
raise NotImplementedError("must not call this")
class TodoRecursiveView(viewsets.ModelViewSet):