Skip to content

Commit f4d62c5

Browse files
committed
Add DefaultSchema utility
1 parent 4b09e82 commit f4d62c5

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

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 in with 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ class CustomView(APIView):
536536
assert isinstance(view.schema, CustomViewInspector)
537537

538538
def test_set_custom_inspector_class_via_settings(self):
539-
with override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS':'tests.test_schemas.CustomViewInspector'}):
539+
with override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'tests.test_schemas.CustomViewInspector'}):
540540
view = APIView()
541541
assert isinstance(view.schema, CustomViewInspector)
542542

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