Catch APIException in doc generation (#5443)

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.
This commit is contained in:
Sigve Sebastian Farstad 2017-09-25 18:28:36 +02:00 committed by Carlton Gibson
parent 8812e6c47a
commit bf0fbd5df1

View File

@ -4,13 +4,14 @@ inspectors.py # Per-endpoint view introspection
See schemas.__init__.py for package overview. See schemas.__init__.py for package overview.
""" """
import re import re
import warnings
from collections import OrderedDict from collections import OrderedDict
from django.db import models from django.db import models
from django.utils.encoding import force_text, smart_text from django.utils.encoding import force_text, smart_text
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers from rest_framework import exceptions, serializers
from rest_framework.compat import coreapi, coreschema, uritemplate, urlparse from rest_framework.compat import coreapi, coreschema, uritemplate, urlparse
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
from rest_framework.utils import formatting from rest_framework.utils import formatting
@ -285,7 +286,14 @@ class AutoSchema(ViewInspector):
if not hasattr(view, 'get_serializer'): if not hasattr(view, 'get_serializer'):
return [] return []
serializer = view.get_serializer() try:
serializer = view.get_serializer()
except exceptions.APIException:
serializer = None
warnings.warn('{}.get_serializer() raised an exception during '
'schema generation. Serializer fields will not be '
'generated for {} {}.'.format(
type(view), method, path))
if isinstance(serializer, serializers.ListSerializer): if isinstance(serializer, serializers.ListSerializer):
return [ return [