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

Commit

Permalink
Use DRF get_schema_view shortcut released in 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
marcgibbons committed Oct 29, 2016
1 parent 267eeab commit 0edee4c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 90 deletions.
3 changes: 2 additions & 1 deletion example_app/snippets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def highlight(self, request, *args, **kwargs):

def perform_create(self, serializer):
serializer.save(owner=self.request.user)



class UserViewSet(viewsets.ReadOnlyModelViewSet):
"""
This endpoint presents the users in the system.
Expand Down
31 changes: 6 additions & 25 deletions rest_framework_swagger/views.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
from rest_framework import exceptions
from rest_framework.permissions import AllowAny
from rest_framework import schemas
from rest_framework.renderers import CoreJSONRenderer
from rest_framework.response import Response
from rest_framework.schemas import SchemaGenerator
from rest_framework.views import APIView

from . import renderers


def get_swagger_view(title=None, url=None):
"""
Returns schema view which renders Swagger/OpenAPI.
(Replace with DRF get_schema_view shortcut in 3.5)
"""
class SwaggerSchemaView(APIView):
_ignore_model_permissions = True
exclude_from_schema = True
permission_classes = [AllowAny]
renderer_classes = [
return schemas.get_schema_view(
title=title,
url=url,
renderer_classes=[
CoreJSONRenderer,
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
]

def get(self, request):
generator = SchemaGenerator(title=title, url=url)
schema = generator.get_schema(request=request)

if not schema:
raise exceptions.ValidationError(
'The schema generator did not return a schema Document'
)

return Response(schema)

return SwaggerSchemaView.as_view()
)
81 changes: 18 additions & 63 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.test import TestCase
from rest_framework.permissions import AllowAny
from rest_framework import schemas
from rest_framework.renderers import CoreJSONRenderer
from rest_framework.request import Request
from rest_framework.test import APIRequestFactory

from rest_framework_swagger import renderers
Expand All @@ -16,66 +15,22 @@ def setUp(self):
self.factory = APIRequestFactory()
self.view_class = self.sut().cls

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

with patch('rest_framework_swagger.views.SchemaGenerator') as mock:
request = self.factory.get('/')
view(request=request)

mock.assert_called_once_with(title=title, url=url)

def test_ignore_model_permissions_true(self):
self.assertTrue(self.view_class._ignore_model_permissions)

def test_exclude_from_schema(self):
self.assertTrue(self.view_class.exclude_from_schema)

def test_renderer_classes(self):
self.assertListEqual(
[
CoreJSONRenderer,
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
],
self.view_class.renderer_classes
)

def test_permission_class(self):
self.assertListEqual(
[AllowAny],
self.view_class.permission_classes
def test_returns_get_schema_view(self):
title = 'Vandelay Industries API',
url = 'http://vandelay.industries'
renderer_classes = [
CoreJSONRenderer,
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
]

with patch.object(schemas, 'get_schema_view') as mock:
result = self.sut(title=title, url=url)

mock.assert_called_once_with(
title=title,
url=url,
renderer_classes=renderer_classes
)

def test_return_400_if_schema_is_none(self):
with patch('rest_framework_swagger.views.SchemaGenerator') as mock:
mock.return_value.get_schema.return_value = None
request = self.factory.get('/')
response = self.sut()(request=request)

self.assertEqual(400, response.status_code)
self.assertEqual(
['The schema generator did not return a schema Document'],
response.data
)

def test_response_is_result_of_schema_generator(self):
expected = 'My amazing schema'
with patch('rest_framework_swagger.views.SchemaGenerator') as mock:
mock.return_value.get_schema.return_value = expected
request = self.factory.get('/')
response = self.sut()(request=request)

self.assertEqual(200, response.status_code)
self.assertEqual(expected, response.data)

def test_schema_generator_instantiated_with_request(self):
with patch('rest_framework_swagger.views.SchemaGenerator') as mock:
request = self.factory.get('/')
self.sut()(request=request)

call_args = mock.return_value.get_schema.call_args[1]
self.assertIn('request', call_args)
self.assertIsInstance(call_args['request'], Request)
self.assertEqual(mock.return_value, result)
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ deps =
mock
openapi-codec
simplejson
drf34: djangorestframework==3.4.7
drf35: djangorestframework>=3.5.1,<3.6
django18: Django>=1.8,<1.9
django19: Django>=1.9,<1.10
Expand Down

0 comments on commit 0edee4c

Please sign in to comment.