Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

Added possibility to use custom schema generator #699

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions rest_framework_swagger/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from . import renderers


def get_swagger_view(title=None, url=None, patterns=None, urlconf=None):
def get_swagger_view(title=None, url=None, patterns=None, urlconf=None,
schema_generator_cls=None):
"""
Returns schema view which renders Swagger/OpenAPI.
"""
Expand All @@ -23,7 +24,12 @@ class SwaggerSchemaView(APIView):
]

def get(self, request):
generator = SchemaGenerator(
if schema_generator_cls is None:
generator_cls = SchemaGenerator
else:
generator_cls = schema_generator_cls
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to make SchemaGenerator default parameter value instead of this condition?


generator = generator_cls(
title=title,
url=url,
patterns=patterns,
Expand Down
27 changes: 26 additions & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework_swagger import renderers
from rest_framework_swagger.views import get_swagger_view

from .compat.mock import patch
from .compat.mock import patch, MagicMock


class TestGetSwaggerView(TestCase):
Expand All @@ -16,6 +16,31 @@ def setUp(self):
self.factory = APIRequestFactory()
self.view_class = self.sut().cls

def test_custom_schema_generator_used(self):
title = 'Vandelay'
url = 'https://github.com/marcgibbons/django-rest-swagger'
urlconf = 'fizz'
patterns = []

schema_generator = MagicMock()
view = self.sut(
title=title,
url=url,
patterns=patterns,
urlconf=urlconf,
schema_generator_cls=schema_generator
)

request = self.factory.get('/')
view(request=request)

schema_generator.assert_called_once_with(
title=title,
url=url,
patterns=patterns,
urlconf=urlconf
)

def test_title_and_urlpassed_to_schema_generator(self):
title = 'Vandelay'
url = 'https://github.com/marcgibbons/django-rest-swagger'
Expand Down