Fix schema base paths (#4611)

This commit is contained in:
Tom Christie 2016-10-21 16:59:34 +01:00 committed by GitHub
parent d647d37a99
commit 0fe0e1e703
2 changed files with 24 additions and 2 deletions

View File

@ -1,4 +1,3 @@
import os
import re
from collections import OrderedDict
from importlib import import_module
@ -37,6 +36,18 @@ types_lookup = ClassLookupDict({
})
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
@ -292,7 +303,7 @@ class SchemaGenerator(object):
# 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):
"""

View File

@ -335,3 +335,14 @@ class TestSchemaGeneratorNotAtRoot(TestCase):
}
)
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 == '/'