From ef9dbb7a0cc4c7031dbf9a7bff02719d8a65a008 Mon Sep 17 00:00:00 2001 From: guacs <126393040+guacs@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:44:47 +0530 Subject: [PATCH] fix: use the full path for fetching openapi.json (#3196) * fix: use the full path for fetching openapi.json * style: dont specify generic type in Final --- litestar/openapi/controller.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/litestar/openapi/controller.py b/litestar/openapi/controller.py index 3f8f653c5f..ac03d4cd15 100644 --- a/litestar/openapi/controller.py +++ b/litestar/openapi/controller.py @@ -1,7 +1,7 @@ from __future__ import annotations from functools import cached_property -from typing import TYPE_CHECKING, Any, Callable, Literal +from typing import TYPE_CHECKING, Any, Callable, Final, Literal from yaml import dump as dump_yaml @@ -22,6 +22,8 @@ from litestar.connection.request import Request from litestar.openapi.spec.open_api import OpenAPI +_OPENAPI_JSON_ROUTER_NAME: Final = "__litestar_openapi_json" + class OpenAPIController(Controller): """Controller for OpenAPI endpoints.""" @@ -171,7 +173,13 @@ def retrieve_schema_yaml(self, request: Request[Any, Any, Any]) -> ASGIResponse: return ASGIResponse(body=self._dumped_yaml_schema, media_type=OpenAPIMediaType.OPENAPI_YAML) return ASGIResponse(body=b"", status_code=HTTP_404_NOT_FOUND, media_type=MediaType.HTML) - @get(path="/openapi.json", media_type=OpenAPIMediaType.OPENAPI_JSON, include_in_schema=False, sync_to_thread=False) + @get( + path="/openapi.json", + media_type=OpenAPIMediaType.OPENAPI_JSON, + include_in_schema=False, + sync_to_thread=False, + name=_OPENAPI_JSON_ROUTER_NAME, + ) def retrieve_schema_json(self, request: Request[Any, Any, Any]) -> ASGIResponse: """Return the OpenAPI schema as JSON with an ``application/vnd.oai.openapi+json`` Content-Type header. @@ -457,10 +465,10 @@ def render_stoplight_elements(self, request: Request[Any, Any, Any]) -> bytes: """ - body = """ + body = f"""