From 3feea10bae65fcefa4c0f9723a2d61f257f62588 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Thu, 31 May 2018 12:03:13 -0400 Subject: [PATCH] Fix schema disabling for extra actions --- rest_framework/schemas/generators.py | 4 ++++ rest_framework/schemas/inspectors.py | 3 ++- tests/test_schemas.py | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/rest_framework/schemas/generators.py b/rest_framework/schemas/generators.py index 3d46251b9..8794c9967 100644 --- a/rest_framework/schemas/generators.py +++ b/rest_framework/schemas/generators.py @@ -218,6 +218,10 @@ class EndpointEnumerator(object): if callback.cls.schema is None: return False + if 'schema' in callback.initkwargs: + if callback.initkwargs['schema'] is None: + return False + if path.endswith('.{format}') or path.endswith('.{format}/'): return False # Ignore .json style URLs. diff --git a/rest_framework/schemas/inspectors.py b/rest_framework/schemas/inspectors.py index 5c9659a57..b90f60e08 100644 --- a/rest_framework/schemas/inspectors.py +++ b/rest_framework/schemas/inspectors.py @@ -157,7 +157,8 @@ class ViewInspector(object): def __set__(self, instance, other): self.instance_schemas[instance] = other - other.view = instance + if other is not None: + other.view = instance @property def view(self): diff --git a/tests/test_schemas.py b/tests/test_schemas.py index c0d6deca1..fdaee0c10 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -99,6 +99,10 @@ class ExampleViewSet(ModelViewSet): def custom_list_action_multiple_methods(self, request): return super(ExampleViewSet, self).list(self, request) + @action(detail=False, schema=None) + def excluded_action(self, request): + pass + def get_serializer(self, *args, **kwargs): assert self.request assert self.action @@ -745,6 +749,20 @@ class TestAutoSchema(TestCase): assert len(fields) == 2 assert "my_extra_field" in [f.name for f in fields] + @pytest.mark.skipif(not coreapi, reason='coreapi is not installed') + def test_viewset_action_with_null_schema(self): + class CustomViewSet(GenericViewSet): + @action(detail=True, schema=None) + def extra_action(self, pk, **kwargs): + pass + + router = SimpleRouter() + router.register(r'detail', CustomViewSet, base_name='detail') + + generator = SchemaGenerator() + view = generator.create_view(router.urls[0].callback, 'GET') + assert view.schema is None + @pytest.mark.skipif(not coreapi, reason='coreapi is not installed') def test_view_with_manual_schema(self):