From 0fe0e1e7038e74f071e93a77bd5900dc191b086d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 21 Oct 2016 16:59:34 +0100 Subject: [PATCH] Fix schema base paths (#4611) --- rest_framework/schemas.py | 15 +++++++++++++-- tests/test_schemas.py | 11 +++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index af861426c1..9b9984699c 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -1,4 +1,3 @@ -import os import re from collections import OrderedDict from importlib import import_module @@ -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 @@ -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): """ diff --git a/tests/test_schemas.py b/tests/test_schemas.py index 7188087c4e..80b456ea07 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -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 == '/'