Skip to content

Commit

Permalink
Fix schema base paths (#4611)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie authored Oct 21, 2016
1 parent d647d37 commit 0fe0e1e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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 == '/'

0 comments on commit 0fe0e1e

Please sign in to comment.