From c4f8718872695d801b4b57a995051bf3844fb4c7 Mon Sep 17 00:00:00 2001 From: Justin Ko Date: Thu, 21 May 2026 21:45:39 +0900 Subject: [PATCH] fix: respect root_path in FastAPI docs --- app/core/config.py | 1 + app/main.py | 1 + tests/test_docs_root_path.py | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 tests/test_docs_root_path.py diff --git a/app/core/config.py b/app/core/config.py index b080ce4..f96cf0e 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -51,6 +51,7 @@ class Settings(BaseSettings): loki_port: int = Field(default=3100, validation_alias="LOKI_PORT") loki_enabled: bool = Field(default=False, validation_alias="LOKI_ENABLED") credential_encryption_key: str = Field(default="", validation_alias="CREDENTIAL_ENCRYPTION_KEY") + root_path: str = Field(default="", validation_alias="PORTFOLIOHUB_ROOT_PATH") model_config = SettingsConfigDict( env_prefix="PORTFOLIOHUB_", diff --git a/app/main.py b/app/main.py index 9033e08..4595c86 100644 --- a/app/main.py +++ b/app/main.py @@ -63,6 +63,7 @@ def create_app( version=settings.app_version, docs_url="/docs", redoc_url="/redoc", + root_path=settings.root_path, lifespan=lifespan, ) diff --git a/tests/test_docs_root_path.py b/tests/test_docs_root_path.py new file mode 100644 index 0000000..d2da782 --- /dev/null +++ b/tests/test_docs_root_path.py @@ -0,0 +1,24 @@ +from fastapi.testclient import TestClient + +from app.main import create_app +from app.core.config import Settings + + +def test_docs_uses_root_path_for_openapi_url() -> None: + app = create_app(settings_factory=lambda: Settings(root_path="/portfolio-hub")) + + with TestClient(app) as client: + response = client.get("/docs") + + assert response.status_code == 200 + assert "url: '/portfolio-hub/openapi.json'" in response.text + + +def test_openapi_is_served_under_root_path() -> None: + app = create_app(settings_factory=lambda: Settings(root_path="/portfolio-hub")) + + with TestClient(app) as client: + response = client.get("/portfolio-hub/openapi.json") + + assert response.status_code == 200 + assert response.json()["info"]["title"] == "PortfolioHub"