Skip to content

Commit 813ec91

Browse files
committed
forward StacApi.title, StacApi.api_version and Stac.Api.description to the FastAPI application
1 parent 2fbcfc0 commit 813ec91

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
lines changed

CHANGES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
- added `query.QueryConformanceClasses` Enum
1919
- added `SortConformanceClasses` Enum
2020

21+
- removed `StacApi.customize_openapi` method
22+
- reordered `StacApi` attributes (moved `title`, `api_version` and `description` before `app`)
23+
24+
### Added
25+
26+
* forward `StacApi.title`, `StacApi.api_version` and `Stac.Api.description` to the FastAPI application
27+
2128
## [4.0.1] - 2025-01-23
2229

2330
### Changed

stac_fastapi/api/stac_fastapi/api/app.py

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"""Fastapi app creation."""
22

33

4-
from typing import Any, Dict, List, Optional, Tuple, Type, Union
4+
from typing import Dict, List, Optional, Tuple, Type, Union
55

66
import attr
77
from brotli_asgi import BrotliMiddleware
88
from fastapi import APIRouter, FastAPI
9-
from fastapi.openapi.utils import get_openapi
109
from fastapi.params import Depends
1110
from stac_pydantic import api
1211
from stac_pydantic.api.collections import Collections
@@ -71,19 +70,6 @@ class StacApi:
7170
exceptions: Dict[Type[Exception], int] = attr.ib(
7271
default=attr.Factory(lambda: DEFAULT_STATUS_CODES)
7372
)
74-
app: FastAPI = attr.ib(
75-
default=attr.Factory(
76-
lambda self: FastAPI(
77-
openapi_url=self.settings.openapi_url,
78-
docs_url=self.settings.docs_url,
79-
redoc_url=None,
80-
root_path=self.settings.root_path,
81-
),
82-
takes_self=True,
83-
),
84-
converter=update_openapi,
85-
)
86-
router: APIRouter = attr.ib(default=attr.Factory(APIRouter))
8773
title: str = attr.ib(
8874
default=attr.Factory(
8975
lambda self: self.settings.stac_fastapi_title, takes_self=True
@@ -100,6 +86,22 @@ class StacApi:
10086
lambda self: self.settings.stac_fastapi_description, takes_self=True
10187
)
10288
)
89+
app: FastAPI = attr.ib(
90+
default=attr.Factory(
91+
lambda self: FastAPI(
92+
openapi_url=self.settings.openapi_url,
93+
docs_url=self.settings.docs_url,
94+
redoc_url=None,
95+
root_path=self.settings.root_path,
96+
title=self.title,
97+
version=self.api_version,
98+
description=self.description,
99+
),
100+
takes_self=True,
101+
),
102+
converter=update_openapi,
103+
)
104+
router: APIRouter = attr.ib(default=attr.Factory(APIRouter))
103105
search_get_request_model: Type[BaseSearchGetRequest] = attr.ib(
104106
default=BaseSearchGetRequest
105107
)
@@ -388,22 +390,6 @@ def register_core(self):
388390
self.register_get_collection()
389391
self.register_get_item_collection()
390392

391-
def customize_openapi(self) -> Optional[Dict[str, Any]]:
392-
"""Customize openapi schema."""
393-
if self.app.openapi_schema:
394-
return self.app.openapi_schema
395-
396-
openapi_schema = get_openapi(
397-
title=self.title,
398-
version=self.api_version,
399-
description=self.description,
400-
routes=self.app.routes,
401-
servers=self.app.servers,
402-
)
403-
404-
self.app.openapi_schema = openapi_schema
405-
return self.app.openapi_schema
406-
407393
def add_health_check(self):
408394
"""Add a health check."""
409395
mgmt_router = APIRouter(prefix=self.app.state.router_prefix)
@@ -467,9 +453,6 @@ def __attrs_post_init__(self):
467453
# register exception handlers
468454
add_exception_handlers(self.app, status_codes=self.exceptions)
469455

470-
# customize openapi
471-
self.app.openapi = self.customize_openapi
472-
473456
# add middlewares
474457
if self.middlewares and self.app.middleware_stack is not None:
475458
raise RuntimeError("Cannot add middleware after an application has started")

stac_fastapi/api/tests/test_app_prefix.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,30 @@ def test_api_prefix_with_root_path(TestCoreClient, prefix):
208208

209209
resp = client.get(prefix + expected_path)
210210
assert resp.status_code == 200
211+
212+
213+
def test_api_fastapi_option(TestCoreClient):
214+
api_settings = ApiSettings(
215+
stac_fastapi_title="stac-fastapi-tests",
216+
stac_fastapi_description="test for stac-fastapi",
217+
stac_fastapi_version="0.1.0dev",
218+
)
219+
220+
api = StacApi(
221+
settings=api_settings,
222+
client=TestCoreClient(),
223+
)
224+
225+
with TestClient(api.app, base_url="http://stac.io") as client:
226+
landing = client.get("/")
227+
assert landing.status_code == 200
228+
body = landing.json()
229+
assert body["title"] == "stac-fastapi-tests"
230+
assert body["description"] == "test for stac-fastapi"
231+
232+
service_desc = client.get("/api")
233+
assert service_desc.status_code == 200
234+
body = service_desc.json()["info"]
235+
assert body["title"] == "stac-fastapi-tests"
236+
assert body["description"] == "test for stac-fastapi"
237+
assert body["version"] == "0.1.0dev"

0 commit comments

Comments
 (0)