Skip to content

Commit 791539a

Browse files
Add DEFAULT_SCHEMA_CLASS setting (#5658)
* Add test for new setting * Add DefaultSchema utility * Add new setting to docs
1 parent 4a200d5 commit 791539a

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

docs/api-guide/settings.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ A content negotiation class, that determines how a renderer is selected for the
9494

9595
Default: `'rest_framework.negotiation.DefaultContentNegotiation'`
9696

97+
#### DEFAULT_SCHEMA_CLASS
98+
99+
A view inspector class that will be used for schema generation.
100+
101+
Default: `'rest_framework.schemas.AutoSchema'`
102+
97103
---
98104

99105
## Generic view settings

rest_framework/schemas/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from rest_framework.settings import api_settings
2424

2525
from .generators import SchemaGenerator
26-
from .inspectors import AutoSchema, ManualSchema # noqa
26+
from .inspectors import AutoSchema, DefaultSchema, ManualSchema # noqa
2727

2828

2929
def get_schema_view(

rest_framework/schemas/inspectors.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
"""
23
inspectors.py # Per-endpoint view introspection
34
@@ -456,3 +457,13 @@ def get_link(self, path, method, base_url):
456457
)
457458

458459
return self._link
460+
461+
462+
class DefaultSchema(object):
463+
"""Allows overriding AutoSchema using DEFAULT_SCHEMA_CLASS setting"""
464+
def __get__(self, instance, owner):
465+
inspector_class = api_settings.DEFAULT_SCHEMA_CLASS
466+
assert issubclass(inspector_class, ViewInspector), "DEFAULT_SCHEMA_CLASS must be set to a ViewInspector (usually an AutoSchema) subclass"
467+
inspector = inspector_class()
468+
inspector.view = instance
469+
return inspector

rest_framework/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
'DEFAULT_PAGINATION_CLASS': None,
5656
'DEFAULT_FILTER_BACKENDS': (),
5757

58+
# Schema
59+
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',
60+
5861
# Throttling
5962
'DEFAULT_THROTTLE_RATES': {
6063
'user': None,
@@ -140,6 +143,7 @@
140143
'DEFAULT_VERSIONING_CLASS',
141144
'DEFAULT_PAGINATION_CLASS',
142145
'DEFAULT_FILTER_BACKENDS',
146+
'DEFAULT_SCHEMA_CLASS',
143147
'EXCEPTION_HANDLER',
144148
'TEST_REQUEST_RENDERER_CLASSES',
145149
'UNAUTHENTICATED_USER',

rest_framework/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from rest_framework import exceptions, status
1919
from rest_framework.request import Request
2020
from rest_framework.response import Response
21-
from rest_framework.schemas import AutoSchema
21+
from rest_framework.schemas import DefaultSchema
2222
from rest_framework.settings import api_settings
2323
from rest_framework.utils import formatting
2424

@@ -117,7 +117,7 @@ class APIView(View):
117117
# Allow dependency injection of other settings to make testing easier.
118118
settings = api_settings
119119

120-
schema = AutoSchema()
120+
schema = DefaultSchema()
121121

122122
@classmethod
123123
def as_view(cls, **initkwargs):

tests/test_schemas.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,30 @@ def test_4605_regression(self):
516516
assert prefix == '/'
517517

518518

519+
class CustomViewInspector(AutoSchema):
520+
"""A dummy AutoSchema subclass"""
521+
pass
522+
523+
519524
class TestAutoSchema(TestCase):
520525

521526
def test_apiview_schema_descriptor(self):
522527
view = APIView()
523528
assert hasattr(view, 'schema')
524529
assert isinstance(view.schema, AutoSchema)
525530

531+
def test_set_custom_inspector_class_on_view(self):
532+
class CustomView(APIView):
533+
schema = CustomViewInspector()
534+
535+
view = CustomView()
536+
assert isinstance(view.schema, CustomViewInspector)
537+
538+
def test_set_custom_inspector_class_via_settings(self):
539+
with override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'tests.test_schemas.CustomViewInspector'}):
540+
view = APIView()
541+
assert isinstance(view.schema, CustomViewInspector)
542+
526543
def test_get_link_requires_instance(self):
527544
descriptor = APIView.schema # Accessed from class
528545
with pytest.raises(AssertionError):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy