Skip to content

Commit f248c5e

Browse files
committed
Add ManualSchema class
1 parent 8f39554 commit f248c5e

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

docs/api-guide/schemas.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,21 @@ Return a list of `coreapi.Link()` instances, as returned by the `get_schema_fiel
463463

464464
Return a list of `coreapi.Link()` instances, as returned by the `get_schema_fields()` method of any filter classes used by the view.
465465

466+
467+
## ManualSchema
468+
469+
`APIViewSchemaDescriptor` subclass for specifying a manual schema.
470+
471+
class MyView(APIView):
472+
schema = ManualSchema(coreapi.Link(
473+
url='/example/',
474+
action='get',
475+
fields=[]
476+
))
477+
478+
The `ManualSchema` constructor takes a single parameter `link`,
479+
the `coreapi.Link` instance for the view.
480+
466481
---
467482

468483
## Core API

rest_framework/schemas.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,18 @@ def get_encoding(self, path, method):
500500
APIView.schema = APIViewSchemaDescriptor()
501501

502502

503+
class ManualSchema(APIViewSchemaDescriptor):
504+
"""
505+
Overrides get_link to return manually specified schema.
506+
"""
507+
def __init__(self, link):
508+
assert isinstance(link, coreapi.Link)
509+
self._link = link
510+
511+
def get_link(self, *args):
512+
return self._link
513+
514+
503515
class SchemaGenerator(object):
504516
# Map HTTP methods onto actions.
505517
default_mapping = {

tests/test_schemas.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from rest_framework.request import Request
1313
from rest_framework.routers import DefaultRouter
1414
from rest_framework.schemas import (
15-
APIViewSchemaDescriptor, SchemaGenerator, get_schema_view
15+
APIViewSchemaDescriptor, ManualSchema, SchemaGenerator, get_schema_view
1616
)
1717
from rest_framework.test import APIClient, APIRequestFactory
1818
from rest_framework.views import APIView
@@ -512,3 +512,18 @@ def test_get_link_requires_instance(self):
512512
descriptor = APIView.schema # Accessed from class
513513
with pytest.raises(AssertionError):
514514
descriptor.get_link(None, None, None) # ???: Do the dummy arguments require a tighter assert?
515+
516+
def test_view_with_manual_schema(self):
517+
518+
expected = coreapi.Link(
519+
url='/example/',
520+
action='get',
521+
fields=[]
522+
)
523+
524+
class CustomView(APIView):
525+
schema = ManualSchema(expected)
526+
527+
view = CustomView()
528+
link = view.schema.get_link()
529+
assert link == expected

0 commit comments

Comments
 (0)