diff --git a/swh/web/api/apidoc.py b/swh/web/api/apidoc.py --- a/swh/web/api/apidoc.py +++ b/swh/web/api/apidoc.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2020 The Software Heritage developers +# Copyright (C) 2015-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -15,11 +15,12 @@ import docutils.parsers.rst import docutils.utils +from django.shortcuts import redirect from rest_framework.decorators import api_view from swh.web.api.apiresponse import make_api_response from swh.web.api.apiurls import APIUrls, CategoryId -from swh.web.utils import parse_rst +from swh.web.utils import parse_rst, reverse class _HTTPDomainDocVisitor(docutils.nodes.NodeVisitor): @@ -368,11 +369,22 @@ return make_api_response(request, None, doc_data) route_name = "%s-doc" % route[1:-1].replace("/", "-") - urlpattern = f"^{api_version}{route}doc/$" + urlpattern = f"^api/{api_version}{route}doc/$" view_name = "api-%s-%s" % (api_version, route_name) APIUrls.add_url_pattern(urlpattern, doc_view, view_name) + # for backward compatibility as previous apidoc URLs were missing + # the /api prefix + old_view_name = view_name.replace("api-", "") + old_urlpattern = f"^{api_version}{route}doc/$" + + @api_view(["GET", "HEAD"]) + def old_doc_view(request): + return redirect(reverse(view_name)) + + APIUrls.add_url_pattern(old_urlpattern, old_doc_view, old_view_name) + @wraps(f) def documented_view(request, **kwargs): doc_data = get_doc_data(f, route, noargs) diff --git a/swh/web/api/apiresponse.py b/swh/web/api/apiresponse.py --- a/swh/web/api/apiresponse.py +++ b/swh/web/api/apiresponse.py @@ -1,4 +1,4 @@ -# Copyright (C) 2017-2021 The Software Heritage developers +# Copyright (C) 2017-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -10,6 +10,7 @@ from django.http import HttpResponse from django.shortcuts import render +from django.urls import get_resolver from django.utils.cache import add_never_cache_headers from django.utils.html import escape from rest_framework.exceptions import APIException @@ -152,9 +153,16 @@ # generate breadcrumbs data if "route" in doc_data: + all_view_names = set(get_resolver().reverse_dict.keys()) doc_data["endpoint_path"] = gen_path_info(doc_data["route"]) for i in range(len(doc_data["endpoint_path"]) - 1): - doc_data["endpoint_path"][i]["path"] += "/doc/" + view_name = "api-1-" + "-".join( + [doc_data["endpoint_path"][i]["name"] for i in range(i + 1)] + ) + if view_name in all_view_names: + doc_data["endpoint_path"][i]["path"] += "/doc/" + else: + doc_data["endpoint_path"][i]["path"] = "" if not doc_data["noargs"]: doc_data["endpoint_path"][-1]["path"] += "/doc/" diff --git a/swh/web/api/templates/apidoc.html b/swh/web/api/templates/apidoc.html --- a/swh/web/api/templates/apidoc.html +++ b/swh/web/api/templates/apidoc.html @@ -1,7 +1,7 @@ {% extends "layout.html" %} {% comment %} -Copyright (C) 2015-2020 The Software Heritage developers +Copyright (C) 2015-2022 The Software Heritage developers See the AUTHORS file at the top-level directory of this distribution License: GNU Affero General Public License version 3, or any later version See top-level LICENSE file for more information @@ -19,7 +19,7 @@