From bf0fbd5df174faf9e5399263c01d21e634470acf Mon Sep 17 00:00:00 2001 From: Sigve Sebastian Farstad Date: Mon, 25 Sep 2017 18:28:36 +0200 Subject: [PATCH] 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. --- rest_framework/schemas/inspectors.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/rest_framework/schemas/inspectors.py b/rest_framework/schemas/inspectors.py index a91205cde..7f2c1cc71 100644 --- a/rest_framework/schemas/inspectors.py +++ b/rest_framework/schemas/inspectors.py @@ -4,13 +4,14 @@ inspectors.py # Per-endpoint view introspection See schemas.__init__.py for package overview. """ import re +import warnings from collections import OrderedDict from django.db import models from django.utils.encoding import force_text, smart_text 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.settings import api_settings from rest_framework.utils import formatting @@ -285,7 +286,14 @@ class AutoSchema(ViewInspector): if not hasattr(view, 'get_serializer'): 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): return [