From bc3e70ded497604bf490c8d09bc9126b555b7ae1 Mon Sep 17 00:00:00 2001 From: Phil Krylov Date: Sat, 13 May 2017 22:40:45 +0300 Subject: [PATCH] Stable/0.3.x: Fix @api_view wrapper detection on DRF 3.4.7+ --- rest_framework_swagger/decorators.py | 14 ++++++++++++++ rest_framework_swagger/docgenerator.py | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/rest_framework_swagger/decorators.py b/rest_framework_swagger/decorators.py index 045d2e3e..a94abe0b 100644 --- a/rest_framework_swagger/decorators.py +++ b/rest_framework_swagger/decorators.py @@ -54,3 +54,17 @@ def wrapper_to_func(wrapper): def func_to_wrapper(func): return func.cls + + +def is_api_view_wrapper(wrapper): + try: + noms = wrapper.http_method_names + handlers = [getattr(wrapper, m) for m in noms if m != 'options'] + except AttributeError: + return False + if not handlers or handlers.count(handlers[0]) != len(handlers): + return False + code = six.get_function_code(handlers[0]) + return (code.co_filename.endswith('decorators.py') and + code.co_name == 'handler' and + code.co_freevars == ('func',)) diff --git a/rest_framework_swagger/docgenerator.py b/rest_framework_swagger/docgenerator.py index f83174b0..0f6ca46d 100644 --- a/rest_framework_swagger/docgenerator.py +++ b/rest_framework_swagger/docgenerator.py @@ -16,6 +16,7 @@ get_primitive_type ) from .compat import OrderedDict +from .decorators import is_api_view_wrapper class DocumentationGenerator(object): @@ -59,7 +60,7 @@ def get_introspector(self, api, apis): path = api['path'] pattern = api['pattern'] callback = api['callback'] - if callback.__module__ == 'rest_framework.decorators': + if is_api_view_wrapper(callback): return WrappedAPIViewIntrospector(callback, path, pattern, self.user) elif issubclass(callback, viewsets.ViewSetMixin): patterns = [a['pattern'] for a in apis