mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-02 03:20:12 +03:00
Add Open API get_schema().
This commit is contained in:
parent
740424e7ae
commit
d99550e54c
|
@ -283,9 +283,7 @@ class SchemaGenerator(object):
|
||||||
"""
|
"""
|
||||||
Generate a `coreapi.Document` representing the API schema.
|
Generate a `coreapi.Document` representing the API schema.
|
||||||
"""
|
"""
|
||||||
if self.endpoints is None:
|
self._initialise_endpoints()
|
||||||
inspector = self.endpoint_inspector_cls(self.patterns, self.urlconf)
|
|
||||||
self.endpoints = inspector.get_api_endpoints()
|
|
||||||
|
|
||||||
links = self.get_links(None if public else request)
|
links = self.get_links(None if public else request)
|
||||||
if not links:
|
if not links:
|
||||||
|
@ -301,6 +299,11 @@ class SchemaGenerator(object):
|
||||||
url=url, content=links
|
url=url, content=links
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _initialise_endpoints(self):
|
||||||
|
if self.endpoints is None:
|
||||||
|
inspector = self.endpoint_inspector_cls(self.patterns, self.urlconf)
|
||||||
|
self.endpoints = inspector.get_api_endpoints()
|
||||||
|
|
||||||
def get_links(self, request=None):
|
def get_links(self, request=None):
|
||||||
"""
|
"""
|
||||||
Return a dictionary containing all the links that should be
|
Return a dictionary containing all the links that should be
|
||||||
|
@ -491,3 +494,20 @@ class OpenAPISchemaGenerator(SchemaGenerator):
|
||||||
result[subpath][method.lower()] = operation
|
result[subpath][method.lower()] = operation
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def get_schema(self, request=None, public=False):
|
||||||
|
"""
|
||||||
|
Generate a `coreapi.Document` representing the API schema.
|
||||||
|
"""
|
||||||
|
self._initialise_endpoints()
|
||||||
|
|
||||||
|
paths = self.get_paths(None if public else request)
|
||||||
|
if not paths:
|
||||||
|
return None
|
||||||
|
|
||||||
|
schema = {
|
||||||
|
'basePath': self.url,
|
||||||
|
'paths': paths,
|
||||||
|
}
|
||||||
|
|
||||||
|
return schema
|
||||||
|
|
|
@ -47,14 +47,12 @@ class TestGenerator(TestCase):
|
||||||
assert isinstance(views.ExampleListView.schema, OpenAPIAutoSchema)
|
assert isinstance(views.ExampleListView.schema, OpenAPIAutoSchema)
|
||||||
|
|
||||||
def test_paths_construction(self):
|
def test_paths_construction(self):
|
||||||
|
"""Construction of the `paths` key."""
|
||||||
patterns = [
|
patterns = [
|
||||||
url(r'^example/?$', views.ExampleListView.as_view()),
|
url(r'^example/?$', views.ExampleListView.as_view()),
|
||||||
]
|
]
|
||||||
generator = OpenAPISchemaGenerator(patterns=patterns)
|
generator = OpenAPISchemaGenerator(patterns=patterns)
|
||||||
|
generator._initialise_endpoints()
|
||||||
# This happens in get_schema()
|
|
||||||
inspector = generator.endpoint_inspector_cls(generator.patterns, generator.urlconf)
|
|
||||||
generator.endpoints = inspector.get_api_endpoints()
|
|
||||||
|
|
||||||
paths = generator.get_paths()
|
paths = generator.get_paths()
|
||||||
|
|
||||||
|
@ -63,3 +61,16 @@ class TestGenerator(TestCase):
|
||||||
assert len(example_operations) == 2
|
assert len(example_operations) == 2
|
||||||
assert 'get' in example_operations
|
assert 'get' in example_operations
|
||||||
assert 'post' in example_operations
|
assert 'post' in example_operations
|
||||||
|
|
||||||
|
def test_schema_construction(self):
|
||||||
|
"""Construction of the top level dictionary."""
|
||||||
|
patterns = [
|
||||||
|
url(r'^example/?$', views.ExampleListView.as_view()),
|
||||||
|
]
|
||||||
|
generator = OpenAPISchemaGenerator(patterns=patterns)
|
||||||
|
|
||||||
|
request = create_request('/')
|
||||||
|
schema = generator.get_schema(request=request)
|
||||||
|
|
||||||
|
assert 'basePath' in schema
|
||||||
|
assert 'paths' in schema
|
||||||
|
|
Loading…
Reference in New Issue
Block a user