From 5c9ed717fc079e70c8ac48a682e4f661dff5bcf6 Mon Sep 17 00:00:00 2001 From: Alan Crosswell Date: Sun, 2 Jun 2019 10:56:50 -0400 Subject: [PATCH] fix // path prefix --- rest_framework/schemas/generators.py | 2 +- rest_framework/schemas/openapi.py | 4 +++- tests/schemas/test_openapi.py | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rest_framework/schemas/generators.py b/rest_framework/schemas/generators.py index ecb07f935..392d385d8 100644 --- a/rest_framework/schemas/generators.py +++ b/rest_framework/schemas/generators.py @@ -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: diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 44b281be8..c03205c3e 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -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 diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 2ddf54f01..4914c1422 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -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 = [