diff --git a/CHANGES.md b/CHANGES.md index 3aee38ea8..bbb5409f4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -18,6 +18,13 @@ - added `query.QueryConformanceClasses` Enum - added `SortConformanceClasses` Enum +- removed `StacApi.customize_openapi` method +- reordered `StacApi` attributes (moved `title`, `api_version` and `description` before `app`) + +### Added + +* forward `StacApi.title`, `StacApi.api_version` and `Stac.Api.description` to the FastAPI application + ## [4.0.1] - 2025-01-23 ### Changed diff --git a/stac_fastapi/api/stac_fastapi/api/app.py b/stac_fastapi/api/stac_fastapi/api/app.py index 22c85f6e2..3e5d74a1e 100644 --- a/stac_fastapi/api/stac_fastapi/api/app.py +++ b/stac_fastapi/api/stac_fastapi/api/app.py @@ -1,12 +1,11 @@ """Fastapi app creation.""" -from typing import Any, Dict, List, Optional, Tuple, Type, Union +from typing import Dict, List, Optional, Tuple, Type, Union import attr from brotli_asgi import BrotliMiddleware from fastapi import APIRouter, FastAPI -from fastapi.openapi.utils import get_openapi from fastapi.params import Depends from stac_pydantic import api from stac_pydantic.api.collections import Collections @@ -71,19 +70,6 @@ class StacApi: exceptions: Dict[Type[Exception], int] = attr.ib( default=attr.Factory(lambda: DEFAULT_STATUS_CODES) ) - app: FastAPI = attr.ib( - default=attr.Factory( - lambda self: FastAPI( - openapi_url=self.settings.openapi_url, - docs_url=self.settings.docs_url, - redoc_url=None, - root_path=self.settings.root_path, - ), - takes_self=True, - ), - converter=update_openapi, - ) - router: APIRouter = attr.ib(default=attr.Factory(APIRouter)) title: str = attr.ib( default=attr.Factory( lambda self: self.settings.stac_fastapi_title, takes_self=True @@ -100,6 +86,22 @@ class StacApi: lambda self: self.settings.stac_fastapi_description, takes_self=True ) ) + app: FastAPI = attr.ib( + default=attr.Factory( + lambda self: FastAPI( + openapi_url=self.settings.openapi_url, + docs_url=self.settings.docs_url, + redoc_url=None, + root_path=self.settings.root_path, + title=self.title, + version=self.api_version, + description=self.description, + ), + takes_self=True, + ), + converter=update_openapi, + ) + router: APIRouter = attr.ib(default=attr.Factory(APIRouter)) search_get_request_model: Type[BaseSearchGetRequest] = attr.ib( default=BaseSearchGetRequest ) @@ -388,22 +390,6 @@ def register_core(self): self.register_get_collection() self.register_get_item_collection() - def customize_openapi(self) -> Optional[Dict[str, Any]]: - """Customize openapi schema.""" - if self.app.openapi_schema: - return self.app.openapi_schema - - openapi_schema = get_openapi( - title=self.title, - version=self.api_version, - description=self.description, - routes=self.app.routes, - servers=self.app.servers, - ) - - self.app.openapi_schema = openapi_schema - return self.app.openapi_schema - def add_health_check(self): """Add a health check.""" mgmt_router = APIRouter(prefix=self.app.state.router_prefix) @@ -467,9 +453,6 @@ def __attrs_post_init__(self): # register exception handlers add_exception_handlers(self.app, status_codes=self.exceptions) - # customize openapi - self.app.openapi = self.customize_openapi - # add middlewares if self.middlewares and self.app.middleware_stack is not None: raise RuntimeError("Cannot add middleware after an application has started") diff --git a/stac_fastapi/api/tests/test_app_prefix.py b/stac_fastapi/api/tests/test_app_prefix.py index 0c5db5774..196e46135 100644 --- a/stac_fastapi/api/tests/test_app_prefix.py +++ b/stac_fastapi/api/tests/test_app_prefix.py @@ -208,3 +208,30 @@ def test_api_prefix_with_root_path(TestCoreClient, prefix): resp = client.get(prefix + expected_path) assert resp.status_code == 200 + + +def test_api_fastapi_option(TestCoreClient): + api_settings = ApiSettings( + stac_fastapi_title="stac-fastapi-tests", + stac_fastapi_description="test for stac-fastapi", + stac_fastapi_version="0.1.0dev", + ) + + api = StacApi( + settings=api_settings, + client=TestCoreClient(), + ) + + with TestClient(api.app, base_url="http://stac.io") as client: + landing = client.get("/") + assert landing.status_code == 200 + body = landing.json() + assert body["title"] == "stac-fastapi-tests" + assert body["description"] == "test for stac-fastapi" + + service_desc = client.get("/api") + assert service_desc.status_code == 200 + body = service_desc.json()["info"] + assert body["title"] == "stac-fastapi-tests" + assert body["description"] == "test for stac-fastapi" + assert body["version"] == "0.1.0dev"