mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-26 16:30:41 +03:00
Fix schema base paths (#4611)
This commit is contained in:
parent
d647d37a99
commit
0fe0e1e703
|
@ -1,4 +1,3 @@
|
||||||
import os
|
|
||||||
import re
|
import re
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from importlib import import_module
|
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):
|
def get_pk_name(model):
|
||||||
meta = model._meta.concrete_model._meta
|
meta = model._meta.concrete_model._meta
|
||||||
return _get_pk(meta).name
|
return _get_pk(meta).name
|
||||||
|
@ -292,7 +303,7 @@ class SchemaGenerator(object):
|
||||||
# one URL that doesn't have a path prefix.
|
# one URL that doesn't have a path prefix.
|
||||||
return '/'
|
return '/'
|
||||||
prefixes.append('/' + prefix + '/')
|
prefixes.append('/' + prefix + '/')
|
||||||
return os.path.commonprefix(prefixes)
|
return common_path(prefixes)
|
||||||
|
|
||||||
def create_view(self, callback, method, request=None):
|
def create_view(self, callback, method, request=None):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -335,3 +335,14 @@ class TestSchemaGeneratorNotAtRoot(TestCase):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
self.assertEqual(schema, expected)
|
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 == '/'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user