Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KeyError exception for nested resource during generate swagger. #228

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rest_framework_nested/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def get_queryset(self):
"""
queryset = super(NestedViewSetMixin, self).get_queryset()

if getattr(self, 'swagger_fake_view', False):
return queryset

orm_filters = {}
parent_lookup_kwargs = self._get_parent_lookup_kwargs()
for query_param, field_name in parent_lookup_kwargs.items():
Expand Down
11 changes: 11 additions & 0 deletions tests/test_viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rest_framework.serializers import HyperlinkedModelSerializer, ModelSerializer
from rest_framework.viewsets import ModelViewSet
from rest_framework.reverse import reverse as drf_reverse
from rest_framework.schemas import generators

from rest_framework_nested.routers import NestedSimpleRouter
from rest_framework_nested.serializers import NestedHyperlinkedModelSerializer
Expand Down Expand Up @@ -210,3 +211,13 @@ def test_create_child_on_viewset_with_mixin(self):
kwargs={'pk': self.root_1.pk},
request=response.wsgi_request)
self.assertEqual(data['parent'], parent_url)

def test_get_queryset_for_children_resource(self):
gen = generators.BaseSchemaGenerator()
gen._initialise_endpoints()
for path, method, callback in gen.endpoints:
view = gen.create_view(callback, method)
# drf_yasg set swagger_fake_view attribute for all view
setattr(view, 'swagger_fake_view', True)
# no error message should be raised here
view.get_queryset()