From fb19f1fb34610835354d2c550252c7b67e41e560 Mon Sep 17 00:00:00 2001 From: Jake Howard <6527489+RealOrangeOne@users.noreply.github.com> Date: Wed, 23 Jul 2025 09:05:04 +0100 Subject: [PATCH] Have API home redirect to docs --- ninja/openapi/urls.py | 11 +++++------ ninja/openapi/views.py | 5 ++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/ninja/openapi/urls.py b/ninja/openapi/urls.py index 5ec999004..c839a1dab 100644 --- a/ninja/openapi/urls.py +++ b/ninja/openapi/urls.py @@ -1,4 +1,3 @@ -from functools import partial from typing import TYPE_CHECKING, Any, List from django.urls import path @@ -15,11 +14,11 @@ def get_openapi_urls(api: "NinjaAPI") -> List[Any]: result = [] if api.openapi_url: - view = partial(openapi_json, api=api) + view = openapi_json if api.docs_decorator: view = api.docs_decorator(view) # type: ignore result.append( - path(api.openapi_url.lstrip("/"), view, name="openapi-json"), + path(api.openapi_url.lstrip("/"), view, {"api": api}, name="openapi-json"), ) assert ( @@ -27,15 +26,15 @@ def get_openapi_urls(api: "NinjaAPI") -> List[Any]: ), "Please use different urls for openapi_url and docs_url" if api.docs_url: - view = partial(openapi_view, api=api) + view = openapi_view if api.docs_decorator: view = api.docs_decorator(view) # type: ignore result.append( - path(api.docs_url.lstrip("/"), view, name="openapi-view"), + path(api.docs_url.lstrip("/"), view, {"api": api}, name="openapi-view"), ) return result def get_root_url(api: "NinjaAPI") -> Any: - return path("", partial(default_home, api=api), name="api-root") + return path("", default_home, {"api": api}, name="api-root") diff --git a/ninja/openapi/views.py b/ninja/openapi/views.py index d2c6660f5..da5633cf0 100644 --- a/ninja/openapi/views.py +++ b/ninja/openapi/views.py @@ -1,6 +1,6 @@ from typing import TYPE_CHECKING, Any, NoReturn -from django.http import Http404, HttpRequest, HttpResponse +from django.http import HttpRequest, HttpResponse, HttpResponseRedirect from ninja.openapi.docs import DocsBase from ninja.responses import Response @@ -12,8 +12,7 @@ def default_home(request: HttpRequest, api: "NinjaAPI", **kwargs: Any) -> NoReturn: "This view is mainly needed to determine the full path for API operations" - docs_url = f"{request.path}{api.docs_url}".replace("//", "/") - raise Http404(f"docs_url = {docs_url}") + return HttpResponseRedirect(f"{request.path}{api.docs_url}".replace("//", "/")) def openapi_json(request: HttpRequest, api: "NinjaAPI", **kwargs: Any) -> HttpResponse: