Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions ninja/openapi/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from functools import partial
from typing import TYPE_CHECKING, Any, List

from django.urls import path
Expand All @@ -15,27 +14,27 @@ 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 (
api.openapi_url != api.docs_url
), "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")
5 changes: 2 additions & 3 deletions ninja/openapi/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down