diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index 875f9454b..056e6263e 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -83,18 +83,6 @@ def field_to_schema(field): return coreschema.String(title=title, description=description) -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 @@ -336,49 +324,24 @@ class SchemaGenerator(object): # Only generate the path prefix for paths that will be included if not paths: return None - prefix = self.determine_path_prefix(paths) for path, method, view in view_endpoints: if not self.has_view_permissions(path, method, view): continue link = self.get_link(path, method, view) - subpath = path[len(prefix):] - keys = self.get_keys(subpath, method, view) + overall = self.make_overall(path) + keys = self.get_keys(path, overall, method, view) insert_into(links, keys, link) return links # Methods used when we generate a view instance from the raw callback... - def determine_path_prefix(self, paths): + def make_overall(self, path): """ - Given a list of all paths, return the common prefix which should be - discounted when generating a schema structure. - - This will be the longest common string that does not include that last - component of the URL, or the last component before a path parameter. - - For example: - - /api/v1/users/ - /api/v1/users/{pk}/ - - The path prefix is '/api/v1/' + Removes '/' from path and returns a linked value using '-'. """ - prefixes = [] - for path in paths: - components = path.strip('/').split('/') - initial_components = [] - for component in components: - if '{' in component: - break - initial_components.append(component) - prefix = '/'.join(initial_components[:-1]) - if not prefix: - # We can just break early in the case that there's at least - # one URL that doesn't have a path prefix. - return '/' - prefixes.append('/' + prefix + '/') - return common_path(prefixes) + ret = [partial for partial in path.split('/') if partial and '{' not in partial] + return '-'.join(ret) def create_view(self, callback, method, request=None): """ @@ -626,7 +589,7 @@ class SchemaGenerator(object): # Method for generating the link layout.... - def get_keys(self, subpath, method, view): + def get_keys(self, path, overall, method, view): """ Return a list of keys that should be used to layout a link within the schema document. @@ -643,14 +606,14 @@ class SchemaGenerator(object): action = view.action else: # Views have no associated action, so we determine one from the method. - if is_list_view(subpath, method, view): + if is_list_view(path, method, view): action = 'list' else: action = self.default_mapping[method.lower()] named_path_components = [ component for component - in subpath.strip('/').split('/') + in overall.strip('/').split('/') if '{' not in component ] @@ -662,8 +625,8 @@ class SchemaGenerator(object): action = self.coerce_method_names[action] return named_path_components + [action] else: + named_path_components = overall.split('-') return named_path_components[:-1] + [action] - if action in self.coerce_method_names: action = self.coerce_method_names[action] diff --git a/rest_framework/templates/rest_framework/docs/document.html b/rest_framework/templates/rest_framework/docs/document.html index ef5f5966b..b37fefe65 100644 --- a/rest_framework/templates/rest_framework/docs/document.html +++ b/rest_framework/templates/rest_framework/docs/document.html @@ -16,7 +16,7 @@ {% for section_key, section in document.data|items %} {% if section_key %} -