Skip to content

Commit e309a4f

Browse files
committed
Fix OpenAPI path generation with common prefixes.
Closes encode#6675. Closes encode#6823.
1 parent 30a21a9 commit e309a4f

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

rest_framework/schemas/openapi.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import warnings
2+
from urllib.parse import urljoin
23

34
from django.core.validators import (
45
DecimalValidator, EmailValidator, MaxLengthValidator, MaxValueValidator,
@@ -39,17 +40,18 @@ def get_paths(self, request=None):
3940
# Only generate the path prefix for paths that will be included
4041
if not paths:
4142
return None
42-
prefix = self.determine_path_prefix(paths)
43-
if prefix == '/': # no prefix
44-
prefix = ''
4543

4644
for path, method, view in view_endpoints:
4745
if not self.has_view_permissions(path, method, view):
4846
continue
4947
operation = view.schema.get_operation(path, method)
50-
subpath = path[len(prefix):]
51-
result.setdefault(subpath, {})
52-
result[subpath][method.lower()] = operation
48+
# Normalise path for any provided mount url.
49+
if path.startswith('/'):
50+
path = path[1:]
51+
path = urljoin(self.url or '/', path)
52+
53+
result.setdefault(path, {})
54+
result[path][method.lower()] = operation
5355

5456
return result
5557

tests/schemas/test_openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def test_mount_url_prefixed_to_paths(self):
247247
url(r'^example/?$', views.ExampleListView.as_view()),
248248
url(r'^example/{pk}/?$', views.ExampleDetailView.as_view()),
249249
]
250-
generator = SchemaGenerator(patterns=patterns, url='/api/')
250+
generator = SchemaGenerator(patterns=patterns, url='/api')
251251
generator._initialise_endpoints()
252252

253253
paths = generator.get_paths()

0 commit comments

Comments
 (0)