Fix OpenAPI path generation with common prefixes.

Closes #6675. Closes #6823.
This commit is contained in:
Carlton Gibson 2019-07-20 21:01:00 +02:00 committed by Carlton Gibson
parent 30a21a98dc
commit e309a4f0b8
2 changed files with 9 additions and 7 deletions

View File

@ -1,4 +1,5 @@
import warnings
from urllib.parse import urljoin
from django.core.validators import (
DecimalValidator, EmailValidator, MaxLengthValidator, MaxValueValidator,
@ -39,17 +40,18 @@ class SchemaGenerator(BaseSchemaGenerator):
# Only generate the path prefix for paths that will be included
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):]
result.setdefault(subpath, {})
result[subpath][method.lower()] = operation
# 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

View File

@ -247,7 +247,7 @@ class TestGenerator(TestCase):
url(r'^example/?$', views.ExampleListView.as_view()),
url(r'^example/{pk}/?$', views.ExampleDetailView.as_view()),
]
generator = SchemaGenerator(patterns=patterns, url='/api/')
generator = SchemaGenerator(patterns=patterns, url='/api')
generator._initialise_endpoints()
paths = generator.get_paths()