Skip to content

Commit

Permalink
fix: use the full path for fetching openapi.json (#3196)
Browse files Browse the repository at this point in the history
* fix: use the full path for fetching openapi.json

* style: dont specify generic type in Final
  • Loading branch information
guacs authored Mar 14, 2024
1 parent 5bbe168 commit ef9dbb7
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions litestar/openapi/controller.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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."""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -457,10 +465,10 @@ def render_stoplight_elements(self, request: Request[Any, Any, Any]) -> bytes:
</head>
"""

body = """
body = f"""
<body>
<elements-api
apiDescriptionUrl="openapi.json"
apiDescriptionUrl="{request.app.route_reverse(_OPENAPI_JSON_ROUTER_NAME)}"
router="hash"
layout="sidebar"
/>
Expand Down Expand Up @@ -489,9 +497,9 @@ def render_rapidoc(self, request: Request[Any, Any, Any]) -> bytes: # pragma: n
</head>
"""

body = """
body = f"""
<body>
<rapi-doc spec-url="openapi.json" />
<rapi-doc spec-url="{request.app.route_reverse(_OPENAPI_JSON_ROUTER_NAME)}" />
</body>
"""

Expand Down

0 comments on commit ef9dbb7

Please sign in to comment.