Skip to content

Commit 0a5693a

Browse files
authored
Merge pull request #27 from community-of-python/vrslev-patch-8
Fix offline Swagger UI for Litestar
2 parents b969235 + 3d59676 commit 0a5693a

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

microbootstrap/bootstrappers/litestar.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from litestar.config.cors import CORSConfig as LitestarCorsConfig
99
from litestar.contrib.opentelemetry.config import OpenTelemetryConfig as LitestarOpentelemetryConfig
1010
from litestar.contrib.prometheus import PrometheusConfig, PrometheusController
11+
from litestar.openapi.plugins import SwaggerRenderPlugin
1112
from litestar_offline_docs import generate_static_files_config
1213
from sentry_sdk.integrations.litestar import LitestarIntegration
1314

@@ -51,24 +52,30 @@ def bootstrap(self) -> None:
5152
@LitestarBootstrapper.use_instrument()
5253
class LitestarSwaggerInstrument(SwaggerInstrument):
5354
def bootstrap_before(self) -> dict[str, typing.Any]:
54-
class LitestarOpenAPIController(openapi.OpenAPIController):
55-
path = self.instrument_config.swagger_path
56-
if self.instrument_config.swagger_offline_docs:
57-
swagger_ui_standalone_preset_js_url = (
58-
f"{self.instrument_config.service_static_path}/swagger-ui-standalone-preset.js"
59-
)
60-
swagger_bundle_path: str = f"{self.instrument_config.service_static_path}/swagger-ui-bundle.js"
61-
swagger_css_url: str = f"{self.instrument_config.service_static_path}/swagger-ui.css"
55+
render_plugins: typing.Final = (
56+
(
57+
SwaggerRenderPlugin(
58+
js_url=f"{self.instrument_config.service_static_path}/swagger-ui-bundle.js",
59+
css_url=f"{self.instrument_config.service_static_path}/swagger-ui.css",
60+
standalone_preset_js_url=(
61+
f"{self.instrument_config.service_static_path}/swagger-ui-standalone-preset.js"
62+
),
63+
),
64+
)
65+
if self.instrument_config.swagger_offline_docs
66+
else ()
67+
)
6268

6369
openapi_config: typing.Final = openapi.OpenAPIConfig(
70+
path=self.instrument_config.swagger_path,
6471
title=self.instrument_config.service_name,
6572
version=self.instrument_config.service_version,
6673
description=self.instrument_config.service_description,
67-
openapi_controller=LitestarOpenAPIController,
74+
render_plugins=render_plugins,
6875
**self.instrument_config.swagger_extra_params,
6976
)
7077

71-
bootstrap_result = {}
78+
bootstrap_result: typing.Final = {}
7279
if self.instrument_config.swagger_offline_docs:
7380
bootstrap_result["static_files_config"] = [
7481
generate_static_files_config(static_files_handler_path=self.instrument_config.service_static_path),

0 commit comments

Comments
 (0)