From 7a159c2137b396b02ca56d58e086f5ea2c7aaaa6 Mon Sep 17 00:00:00 2001 From: Artur Shiriev Date: Thu, 8 May 2025 13:37:11 +0300 Subject: [PATCH] allow additional kwargs for fastapi --- .../bootstrappers/fastapi_bootstrapper.py | 14 ++++++++++---- tests/test_fastapi_bootstrap.py | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py b/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py index e613193..80456d1 100644 --- a/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py +++ b/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py @@ -39,6 +39,7 @@ class FastAPIConfig( CorsConfig, HealthChecksConfig, LoggingConfig, OpentelemetryConfig, PrometheusConfig, SentryConfig, SwaggerConfig ): application: "fastapi.FastAPI" = dataclasses.field(default=None) # type: ignore[assignment] + application_kwargs: dict[str, typing.Any] = dataclasses.field(default_factory=dict) opentelemetry_excluded_urls: list[str] = dataclasses.field(default_factory=list) prometheus_instrumentator_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict) prometheus_instrument_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict) @@ -46,7 +47,15 @@ class FastAPIConfig( def __post_init__(self) -> None: if not self.application: - object.__setattr__(self, "application", fastapi.FastAPI(docs_url=self.swagger_path)) + object.__setattr__( + self, "application", fastapi.FastAPI(docs_url=self.swagger_path, **self.application_kwargs) + ) + elif self.application_kwargs: + warnings.warn("application_kwargs must be used without application", stacklevel=2) + + self.application.title = self.service_name + self.application.debug = self.service_debug + self.application.version = self.service_version @dataclasses.dataclass(kw_only=True, slots=True, frozen=True) @@ -182,9 +191,6 @@ async def lifespan_manager(self, _: "fastapi.FastAPI") -> typing.AsyncIterator[d def __init__(self, bootstrap_config: FastAPIConfig) -> None: super().__init__(bootstrap_config) - self.bootstrap_config.application.title = bootstrap_config.service_name - self.bootstrap_config.application.debug = bootstrap_config.service_debug - self.bootstrap_config.application.version = bootstrap_config.service_version old_lifespan_manager = self.bootstrap_config.application.router.lifespan_context self.bootstrap_config.application.router.lifespan_context = _merge_lifespan_context( diff --git a/tests/test_fastapi_bootstrap.py b/tests/test_fastapi_bootstrap.py index a3e8685..04720f0 100644 --- a/tests/test_fastapi_bootstrap.py +++ b/tests/test_fastapi_bootstrap.py @@ -71,6 +71,11 @@ def test_fastapi_bootstrapper_docs_url_differ(fastapi_config: FastAPIConfig) -> bootstrapper.bootstrap() +def test_fastapi_bootstrapper_apps_and_kwargs_warning(fastapi_config: FastAPIConfig) -> None: + with pytest.warns(UserWarning, match="application_kwargs must be used without application"): + dataclasses.replace(fastapi_config, application=fastapi.FastAPI(), application_kwargs={"title": "some title"}) + + @pytest.mark.parametrize( "package_name", [