Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lite_bootstrap/bootstrappers/fastapi_bootstrapper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import dataclasses
import typing
import warnings

from lite_bootstrap import import_checker
from lite_bootstrap.bootstrappers.base import BaseBootstrapper
Expand Down Expand Up @@ -35,12 +36,16 @@
class FastAPIConfig(
CorsConfig, HealthChecksConfig, LoggingConfig, OpentelemetryConfig, PrometheusConfig, SentryConfig, SwaggerConfig
):
application: "fastapi.FastAPI" = dataclasses.field(default_factory=lambda: fastapi.FastAPI())
application: "fastapi.FastAPI" = dataclasses.field(default=None) # type: ignore[assignment]
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)
prometheus_expose_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)

def __post_init__(self) -> None:
if not self.application:
object.__setattr__(self, "application", fastapi.FastAPI(docs_url=self.swagger_path))


@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
class FastApiCorsInstrument(CorsInstrument):
Expand Down Expand Up @@ -139,6 +144,12 @@ class FastApiSwaggerInstrument(SwaggerInstrument):
bootstrap_config: FastAPIConfig

def bootstrap(self) -> None:
if self.bootstrap_config.swagger_path != self.bootstrap_config.application.docs_url:
warnings.warn(
f"swagger_path is differ from docs_url, "
f"{self.bootstrap_config.application.docs_url} will be used for docs path",
stacklevel=2,
)
if self.bootstrap_config.swagger_offline_docs:
enable_offline_docs(
self.bootstrap_config.application, static_path=self.bootstrap_config.swagger_static_path
Expand Down
2 changes: 1 addition & 1 deletion lite_bootstrap/bootstrappers/litestar_bootstrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LitestarConfig(
application_config: "AppConfig" = dataclasses.field(default_factory=lambda: AppConfig())
opentelemetry_excluded_urls: list[str] = dataclasses.field(default_factory=list)
prometheus_additional_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)
swagger_path: str = "/docs"
swagger_extra_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)


@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
Expand Down
3 changes: 1 addition & 2 deletions lite_bootstrap/instruments/swagger_instrument.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import dataclasses
import typing

from lite_bootstrap.instruments.base import BaseConfig, BaseInstrument


@dataclasses.dataclass(kw_only=True, frozen=True)
class SwaggerConfig(BaseConfig):
swagger_static_path: str = "/static"
swagger_path: str = "/docs"
swagger_offline_docs: bool = False
swagger_extra_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)


@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_fastapi_bootstrap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import dataclasses

import fastapi
import pytest
import structlog
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
Expand Down Expand Up @@ -61,6 +64,13 @@ def test_fastapi_bootstrapper_not_ready() -> None:
FastAPIBootstrapper(bootstrap_config=FastAPIConfig())


def test_fastapi_bootstrapper_docs_url_differ(fastapi_config: FastAPIConfig) -> None:
new_config = dataclasses.replace(fastapi_config, application=fastapi.FastAPI(docs_url="/custom-docs/"))
bootstrapper = FastAPIBootstrapper(bootstrap_config=new_config)
with pytest.warns(UserWarning, match="swagger_path is differ from docs_url"):
bootstrapper.bootstrap()


@pytest.mark.parametrize(
"package_name",
[
Expand Down
Loading