Corrected openapi.SchemaGenerator path prefixes. (#6724)

This commit is contained in:
Alan Crosswell 2019-06-09 08:29:55 -04:00 committed by Carlton Gibson
parent 60bcc93202
commit a63860fc8b
3 changed files with 18 additions and 2 deletions

View File

@ -249,7 +249,7 @@ class BaseSchemaGenerator(object):
/api/v1/users/
/api/v1/users/{pk}/
The path prefix is '/api/v1/'
The path prefix is '/api/v1'
"""
prefixes = []
for path in paths:

View File

@ -35,12 +35,14 @@ class SchemaGenerator(BaseSchemaGenerator):
if not paths:
return None
prefix = self.determine_path_prefix(paths)
if prefix == '/': # no prefix
prefix = ''
for path, method, view in view_endpoints:
if not self.has_view_permissions(path, method, view):
continue
operation = view.schema.get_operation(path, method)
subpath = '/' + path[len(prefix):]
subpath = path[len(prefix):]
result.setdefault(subpath, {})
result[subpath][method.lower()] = operation

View File

@ -214,6 +214,20 @@ class TestGenerator(TestCase):
assert 'get' in example_operations
assert 'post' in example_operations
def test_prefixed_paths_construction(self):
"""Construction of the `paths` key with a common prefix."""
patterns = [
url(r'^api/v1/example/?$', views.ExampleListView.as_view()),
url(r'^api/v1/example/{pk}/?$', views.ExampleDetailView.as_view()),
]
generator = SchemaGenerator(patterns=patterns)
generator._initialise_endpoints()
paths = generator.get_paths()
assert '/example/' in paths
assert '/example/{id}/' in paths
def test_schema_construction(self):
"""Construction of the top level dictionary."""
patterns = [