Skip to content

Commit deb4484

Browse files
committed
Catch APIException in doc generation
The documentation generator calls view.get_serializer() in order to inspect it for documentation generation. However, if get_serializer() throws an APIException (e.g. PermissionDenied), it doesn't get caught at the call site, but instead propagates up and aborts the entire view. With the try/except in this commit, the documentation generator instead gratiously ignores that particular view and moves on to the next one instead. Practical concequences of this commit is that the docs no longer break if any view's get_serializer(..) throws an APIException.
1 parent f6c19e5 commit deb4484

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

rest_framework/schemas/inspectors.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
See schemas.__init__.py for package overview.
55
"""
66
import re
7+
import warnings
78
from collections import OrderedDict
89

910
from django.db import models
1011
from django.utils.encoding import force_text, smart_text
1112
from django.utils.translation import ugettext_lazy as _
1213

13-
from rest_framework import serializers
14+
from rest_framework import exceptions, serializers
1415
from rest_framework.compat import coreapi, coreschema, uritemplate, urlparse
1516
from rest_framework.settings import api_settings
1617
from rest_framework.utils import formatting
@@ -285,7 +286,14 @@ def get_serializer_fields(self, path, method):
285286
if not hasattr(view, 'get_serializer'):
286287
return []
287288

288-
serializer = view.get_serializer()
289+
try:
290+
serializer = view.get_serializer()
291+
except exceptions.APIException:
292+
serializer = None
293+
warnings.warn('{}.get_serializer() raised an exception during '
294+
'schema generation. Serializer fields will not be '
295+
'generated for {} {}.'.format(
296+
type(view), method, path))
289297

290298
if isinstance(serializer, serializers.ListSerializer):
291299
return [

0 commit comments

Comments
 (0)