Skip to content

Commit

Permalink
Merge pull request #30 from community-of-python/fix-swagger-litestar
Browse files Browse the repository at this point in the history
Use Swagger UI by default for Litestar & allow to override set params in LitestarSwaggerInstrument
  • Loading branch information
vrslev authored Oct 11, 2024
2 parents e5d20cc + 542c213 commit ad9e64e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
26 changes: 12 additions & 14 deletions microbootstrap/bootstrappers/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,25 @@ def bootstrap_before(self) -> dict[str, typing.Any]:
),
)
if self.instrument_config.swagger_offline_docs
else ()
else (SwaggerRenderPlugin(),)
)

openapi_config: typing.Final = openapi.OpenAPIConfig(
path=self.instrument_config.swagger_path,
title=self.instrument_config.service_name,
version=self.instrument_config.service_version,
description=self.instrument_config.service_description,
render_plugins=render_plugins,
**self.instrument_config.swagger_extra_params,
)
all_swagger_params: typing.Final = {
"path": self.instrument_config.swagger_path,
"title": self.instrument_config.service_name,
"version": self.instrument_config.service_version,
"description": self.instrument_config.service_description,
"render_plugins": render_plugins,
} | self.instrument_config.swagger_extra_params

bootstrap_result: typing.Final = {}
bootstrap_result: typing.Final[dict[str, typing.Any]] = {
"openapi_config": openapi.OpenAPIConfig(**all_swagger_params)
}
if self.instrument_config.swagger_offline_docs:
bootstrap_result["static_files_config"] = [
generate_static_files_config(static_files_handler_path=self.instrument_config.service_static_path),
]
return {
**bootstrap_result,
"openapi_config": openapi_config,
}
return bootstrap_result


@LitestarBootstrapper.use_instrument()
Expand Down
13 changes: 13 additions & 0 deletions tests/instruments/test_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import litestar
from httpx import AsyncClient
from litestar import openapi, status_codes
from litestar.openapi.plugins import ScalarRenderPlugin
from litestar.static_files import StaticFilesConfig
from litestar.testing import AsyncTestClient

Expand Down Expand Up @@ -51,6 +52,18 @@ def test_litestar_swagger_bootstrap_online_docs(minimal_swagger_config: SwaggerC
assert "static_files_config" not in bootstrap_result


def test_litestar_swagger_bootstrap_with_overriden_render_plugins(minimal_swagger_config: SwaggerConfig) -> None:
new_render_plugins: typing.Final = [ScalarRenderPlugin()]
minimal_swagger_config.swagger_extra_params["render_plugins"] = new_render_plugins

swagger_instrument: typing.Final = LitestarSwaggerInstrument(minimal_swagger_config)
bootstrap_result: typing.Final = swagger_instrument.bootstrap_before()

assert "openapi_config" in bootstrap_result
assert isinstance(bootstrap_result["openapi_config"], openapi.OpenAPIConfig)
assert bootstrap_result["openapi_config"].render_plugins is new_render_plugins


def test_litestar_swagger_bootstrap_offline_docs(minimal_swagger_config: SwaggerConfig) -> None:
minimal_swagger_config.swagger_offline_docs = True
swagger_instrument: typing.Final = LitestarSwaggerInstrument(minimal_swagger_config)
Expand Down

0 comments on commit ad9e64e

Please sign in to comment.