Skip to content

Commit

Permalink
add drf-yasg compatibility feature 'swagger_fake_view' #321
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Mar 2, 2021
1 parent 4cec67b commit 8c48c49
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/drf_yasg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ only minor modifications.
- ``@swagger_serializer_method`` is equivalent to :py:func:`@extend_schema_field <drf_spectacular.utils.extend_schema_field>`.

- ``ref_name`` on Serializer ``Meta`` classes is supported (excluding inlining with ``ref_name=None``)

- ``swagger_fake_view`` is available as attribute on views to signal schema generation
4 changes: 4 additions & 0 deletions drf_spectacular/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ def create_view(self, callback, method, request=None):

view = super().create_view(callback, method, request)

# drf-yasg compatibility feature. makes the view aware that we are running
# schema generation and not a real request.
view.swagger_fake_view = True

# callback.cls is hosted in urlpatterns and is therefore not an ephemeral modification.
# restore after view creation so potential revisits have a clean state as basis.
if override_view:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,11 +1009,13 @@ class XViewset(viewsets.ReadOnlyModelViewSet):
def get_serializer_class(self):
assert not self.request.user.is_authenticated
assert self.action in ['retrieve', 'list']
assert getattr(self, 'swagger_fake_view', False) # drf-yasg comp
return SimpleSerializer

def get_queryset(self):
assert not self.request.user.is_authenticated
assert self.request.method == 'GET'
assert getattr(self, 'swagger_fake_view', False) # drf-yasg comp
return SimpleModel.objects.none()

generate_schema('x', XViewset)
Expand Down

0 comments on commit 8c48c49

Please sign in to comment.