Skip to content
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

Fix schema base paths #4611

Merged
merged 1 commit into from
Oct 21, 2016
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
15 changes: 13 additions & 2 deletions rest_framework/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import re
from collections import OrderedDict
from importlib import import_module
Expand Down Expand Up @@ -37,6 +36,18 @@
})


def common_path(paths):
split_paths = [path.strip('/').split('/') for path in paths]
s1 = min(split_paths)
s2 = max(split_paths)
common = s1
for i, c in enumerate(s1):
if c != s2[i]:
common = s1[:i]
break
return '/' + '/'.join(common)


def get_pk_name(model):
meta = model._meta.concrete_model._meta
return _get_pk(meta).name
Expand Down Expand Up @@ -292,7 +303,7 @@ def determine_path_prefix(self, paths):
# one URL that doesn't have a path prefix.
return '/'
prefixes.append('/' + prefix + '/')
return os.path.commonprefix(prefixes)
return common_path(prefixes)

def create_view(self, callback, method, request=None):
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,14 @@ def test_schema_for_regular_views(self):
}
)
self.assertEqual(schema, expected)


@unittest.skipUnless(coreapi, 'coreapi is not installed')
class Test4605Regression(TestCase):
def test_4605_regression(self):
generator = SchemaGenerator()
prefix = generator.determine_path_prefix([
'/api/v1/items/',
'/auth/convert-token/'
])
assert prefix == '/'