Skip to content

Add DEFAULT_SCHEMA_CLASS setting #5658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/api-guide/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ A content negotiation class, that determines how a renderer is selected for the

Default: `'rest_framework.negotiation.DefaultContentNegotiation'`

#### DEFAULT_SCHEMA_CLASS

A view inspector class that will be used for schema generation.

Default: `'rest_framework.schemas.AutoSchema'`

---

## Generic view settings
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from rest_framework.settings import api_settings

from .generators import SchemaGenerator
from .inspectors import AutoSchema, ManualSchema # noqa
from .inspectors import AutoSchema, DefaultSchema, ManualSchema # noqa


def get_schema_view(
Expand Down
11 changes: 11 additions & 0 deletions rest_framework/schemas/inspectors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
inspectors.py # Per-endpoint view introspection

Expand Down Expand Up @@ -456,3 +457,13 @@ def get_link(self, path, method, base_url):
)

return self._link


class DefaultSchema(object):
"""Allows overriding AutoSchema using DEFAULT_SCHEMA_CLASS setting"""
def __get__(self, instance, owner):
inspector_class = api_settings.DEFAULT_SCHEMA_CLASS
assert issubclass(inspector_class, ViewInspector), "DEFAULT_SCHEMA_CLASS must be set to a ViewInspector (usually an AutoSchema) subclass"
inspector = inspector_class()
inspector.view = instance
return inspector
4 changes: 4 additions & 0 deletions rest_framework/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
'DEFAULT_PAGINATION_CLASS': None,
'DEFAULT_FILTER_BACKENDS': (),

# Schema
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',

# Throttling
'DEFAULT_THROTTLE_RATES': {
'user': None,
Expand Down Expand Up @@ -140,6 +143,7 @@
'DEFAULT_VERSIONING_CLASS',
'DEFAULT_PAGINATION_CLASS',
'DEFAULT_FILTER_BACKENDS',
'DEFAULT_SCHEMA_CLASS',
'EXCEPTION_HANDLER',
'TEST_REQUEST_RENDERER_CLASSES',
'UNAUTHENTICATED_USER',
Expand Down
4 changes: 2 additions & 2 deletions rest_framework/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from rest_framework import exceptions, status
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.schemas import AutoSchema
from rest_framework.schemas import DefaultSchema
from rest_framework.settings import api_settings
from rest_framework.utils import formatting

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

schema = AutoSchema()
schema = DefaultSchema()

@classmethod
def as_view(cls, **initkwargs):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,30 @@ def test_4605_regression(self):
assert prefix == '/'


class CustomViewInspector(AutoSchema):
"""A dummy AutoSchema subclass"""
pass


class TestAutoSchema(TestCase):

def test_apiview_schema_descriptor(self):
view = APIView()
assert hasattr(view, 'schema')
assert isinstance(view.schema, AutoSchema)

def test_set_custom_inspector_class_on_view(self):
class CustomView(APIView):
schema = CustomViewInspector()

view = CustomView()
assert isinstance(view.schema, CustomViewInspector)

def test_set_custom_inspector_class_via_settings(self):
with override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'tests.test_schemas.CustomViewInspector'}):
view = APIView()
assert isinstance(view.schema, CustomViewInspector)

def test_get_link_requires_instance(self):
descriptor = APIView.schema # Accessed from class
with pytest.raises(AssertionError):
Expand Down
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