Skip to content

OpenAPI endpoint enumeration refactoring. #7127

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
35 changes: 13 additions & 22 deletions rest_framework/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,30 @@ def get_info(self):

return info

def get_paths(self, request=None):
result = {}

paths, view_endpoints = self._get_paths_and_endpoints(request)

# Only generate the path prefix for paths that will be included
if not paths:
return None
def get_schema(self, request=None, public=False):
"""
Generate a OpenAPI schema.
"""
self._initialise_endpoints()

# Iterate endpoints generating per method path operations.
# TODO: …and reference components.
paths = {}
_, view_endpoints = self._get_paths_and_endpoints(None if public else request)
for path, method, view in view_endpoints:
if not self.has_view_permissions(path, method, view):
continue

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I intend for per path, method, view component generation, components (if any) then becoming parameters to get_operation()

operation = view.schema.get_operation(path, method)
# Normalise path for any provided mount url.
if path.startswith('/'):
path = path[1:]
path = urljoin(self.url or '/', path)

result.setdefault(path, {})
result[path][method.lower()] = operation

return result

def get_schema(self, request=None, public=False):
"""
Generate a OpenAPI schema.
"""
self._initialise_endpoints()

paths = self.get_paths(None if public else request)
if not paths:
return None
paths.setdefault(path, {})
paths[path][method.lower()] = operation

# Compile final schema.
schema = {
'openapi': '3.0.2',
'info': self.get_info(),
Expand Down
15 changes: 12 additions & 3 deletions tests/schemas/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ def test_paths_construction(self):
generator = SchemaGenerator(patterns=patterns)
generator._initialise_endpoints()

paths = generator.get_paths()
paths = generator.get_schema()["paths"]

assert '/example/' in paths
example_operations = paths['/example/']
Expand All @@ -676,7 +676,7 @@ def test_prefixed_paths_construction(self):
generator = SchemaGenerator(patterns=patterns)
generator._initialise_endpoints()

paths = generator.get_paths()
paths = generator.get_schema()["paths"]

assert '/v1/example/' in paths
assert '/v1/example/{id}/' in paths
Expand All @@ -689,7 +689,7 @@ def test_mount_url_prefixed_to_paths(self):
generator = SchemaGenerator(patterns=patterns, url='/api')
generator._initialise_endpoints()

paths = generator.get_paths()
paths = generator.get_schema()["paths"]

assert '/api/example/' in paths
assert '/api/example/{id}/' in paths
Expand All @@ -707,6 +707,15 @@ def test_schema_construction(self):
assert 'openapi' in schema
assert 'paths' in schema

def test_schema_with_no_paths(self):
patterns = []
generator = SchemaGenerator(patterns=patterns)

request = create_request('/')
schema = generator.get_schema(request=request)

assert schema['paths'] == {}

def test_schema_information(self):
"""Construction of the top level dictionary."""
patterns = [
Expand Down