Skip to content

Commit bd9a799

Browse files
Fixed SchemaView to reset renderer on exception. (#6429)
Fixes #6258.
1 parent f54a220 commit bd9a799

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

rest_framework/schemas/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ def get(self, request, *args, **kwargs):
3131
if schema is None:
3232
raise exceptions.PermissionDenied()
3333
return Response(schema)
34+
35+
def handle_exception(self, exc):
36+
# Schema renderers do not render exceptions, so re-perform content
37+
# negotiation with default renderers.
38+
self.renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
39+
neg = self.perform_content_negotiation(self.request, force=True)
40+
self.request.accepted_renderer, self.request.accepted_media_type = neg
41+
return super(SchemaView, self).handle_exception(exc)

tests/test_schemas.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,3 +1304,13 @@ def test_foo(self):
13041304

13051305
def test_FOO(self):
13061306
assert not self._test('FOO')
1307+
1308+
1309+
@pytest.mark.skipif(not coreapi, reason='coreapi is not installed')
1310+
def test_schema_handles_exception():
1311+
schema_view = get_schema_view(permission_classes=[DenyAllUsingPermissionDenied])
1312+
request = factory.get('/')
1313+
response = schema_view(request)
1314+
response.render()
1315+
assert response.status_code == 403
1316+
assert "You do not have permission to perform this action." in str(response.content)

0 commit comments

Comments
 (0)