Add ManualSchema class

This commit is contained in:
Carlton Gibson 2017-08-29 11:45:24 +02:00
parent 7aa52aca71
commit 81bac6fe51
3 changed files with 43 additions and 1 deletions

View File

@ -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.
## 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

View File

@ -500,6 +500,18 @@ class APIViewSchemaDescriptor(object):
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):
# Map HTTP methods onto actions.
default_mapping = {

View File

@ -12,7 +12,7 @@ from rest_framework.decorators import detail_route, list_route
from rest_framework.request import Request
from rest_framework.routers import DefaultRouter
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.views import APIView
@ -512,3 +512,18 @@ class TestDescriptor(TestCase):
descriptor = APIView.schema # Accessed from class
with pytest.raises(AssertionError):
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