mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 12:30:11 +03:00
Add ManualSchema
class
This commit is contained in:
parent
7aa52aca71
commit
81bac6fe51
|
@ -463,6 +463,21 @@ Return a list of `coreapi.Link()` instances, as returned by the `get_schema_fiel
|
||||||
|
|
||||||
Return a list of `coreapi.Link()` instances, as returned by the `get_schema_fields()` method of any filter classes used by the view.
|
Return a list of `coreapi.Link()` instances, as returned by the `get_schema_fields()` method of any filter classes used by the view.
|
||||||
|
|
||||||
|
|
||||||
|
## ManualSchema
|
||||||
|
|
||||||
|
`APIViewSchemaDescriptor` subclass for specifying a manual schema.
|
||||||
|
|
||||||
|
class MyView(APIView):
|
||||||
|
schema = ManualSchema(coreapi.Link(
|
||||||
|
url='/example/',
|
||||||
|
action='get',
|
||||||
|
fields=[]
|
||||||
|
))
|
||||||
|
|
||||||
|
The `ManualSchema` constructor takes a single parameter `link`,
|
||||||
|
the `coreapi.Link` instance for the view.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Core API
|
## Core API
|
||||||
|
|
|
@ -500,6 +500,18 @@ class APIViewSchemaDescriptor(object):
|
||||||
APIView.schema = APIViewSchemaDescriptor()
|
APIView.schema = APIViewSchemaDescriptor()
|
||||||
|
|
||||||
|
|
||||||
|
class ManualSchema(APIViewSchemaDescriptor):
|
||||||
|
"""
|
||||||
|
Overrides get_link to return manually specified schema.
|
||||||
|
"""
|
||||||
|
def __init__(self, link):
|
||||||
|
assert isinstance(link, coreapi.Link)
|
||||||
|
self._link = link
|
||||||
|
|
||||||
|
def get_link(self, *args):
|
||||||
|
return self._link
|
||||||
|
|
||||||
|
|
||||||
class SchemaGenerator(object):
|
class SchemaGenerator(object):
|
||||||
# Map HTTP methods onto actions.
|
# Map HTTP methods onto actions.
|
||||||
default_mapping = {
|
default_mapping = {
|
||||||
|
|
|
@ -12,7 +12,7 @@ from rest_framework.decorators import detail_route, list_route
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from rest_framework.routers import DefaultRouter
|
from rest_framework.routers import DefaultRouter
|
||||||
from rest_framework.schemas import (
|
from rest_framework.schemas import (
|
||||||
APIViewSchemaDescriptor, SchemaGenerator, get_schema_view
|
APIViewSchemaDescriptor, ManualSchema, SchemaGenerator, get_schema_view
|
||||||
)
|
)
|
||||||
from rest_framework.test import APIClient, APIRequestFactory
|
from rest_framework.test import APIClient, APIRequestFactory
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
@ -512,3 +512,18 @@ class TestDescriptor(TestCase):
|
||||||
descriptor = APIView.schema # Accessed from class
|
descriptor = APIView.schema # Accessed from class
|
||||||
with pytest.raises(AssertionError):
|
with pytest.raises(AssertionError):
|
||||||
descriptor.get_link(None, None, None) # ???: Do the dummy arguments require a tighter assert?
|
descriptor.get_link(None, None, None) # ???: Do the dummy arguments require a tighter assert?
|
||||||
|
|
||||||
|
def test_view_with_manual_schema(self):
|
||||||
|
|
||||||
|
expected = coreapi.Link(
|
||||||
|
url='/example/',
|
||||||
|
action='get',
|
||||||
|
fields=[]
|
||||||
|
)
|
||||||
|
|
||||||
|
class CustomView(APIView):
|
||||||
|
schema = ManualSchema(expected)
|
||||||
|
|
||||||
|
view = CustomView()
|
||||||
|
link = view.schema.get_link()
|
||||||
|
assert link == expected
|
||||||
|
|
Loading…
Reference in New Issue
Block a user