Skip to content

Commit 25af093

Browse files
authored
fix: fields extension on collections endpoint (#326)
1 parent 990b2af commit 25af093

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

stac_fastapi/pgstac/core.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ async def all_collections( # noqa: C901
150150
prev=prev_link,
151151
).get_links()
152152

153-
return Collections(
153+
collections = Collections(
154154
collections=linked_collections or [],
155155
links=links,
156156
numberMatched=collections_result.get(
@@ -161,6 +161,14 @@ async def all_collections( # noqa: C901
161161
),
162162
)
163163

164+
# If we have the `fields` extension enabled
165+
# we need to avoid Pydantic validation because the
166+
# Items might not be a valid STAC Item objects
167+
if fields:
168+
return JSONResponse(collections) # type: ignore
169+
170+
return collections
171+
164172
async def get_collection(
165173
self,
166174
collection_id: str,
@@ -617,7 +625,7 @@ def _clean_search_args( # noqa: C901
617625
else:
618626
includes.add(field)
619627

620-
base_args["fields"] = {"include": includes, "exclude": excludes}
628+
base_args["fields"] = {"include": list(includes), "exclude": list(excludes)}
621629

622630
if q:
623631
base_args["q"] = q

tests/api/test_api.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,47 @@ async def test_app_query_extension_gte(load_test_data, app_client, load_test_col
271271
assert len(resp_json["features"]) == 1
272272

273273

274+
async def test_app_collection_fields_extension(
275+
load_test_data, app_client, load_test_collection, app
276+
):
277+
fields = ["title"]
278+
resp = await app_client.get("/collections", params={"fields": ",".join(fields)})
279+
280+
assert resp.status_code == 200
281+
282+
resp_json = resp.json()
283+
resp_collections = resp_json["collections"]
284+
285+
assert len(resp_collections) > 0
286+
# NOTE: It's a bug that 'collection' is always included; see #327
287+
constant_fields = ["id", "links", "collection"]
288+
for collection in resp_collections:
289+
assert set(collection.keys()) == set(fields + constant_fields)
290+
291+
292+
async def test_app_item_fields_extension(
293+
load_test_data, app_client, load_test_collection, load_test_item, app
294+
):
295+
coll = load_test_collection
296+
fields = ["id", "geometry"]
297+
resp = await app_client.get(
298+
f"/collections/{coll['id']}/items", params={"fields": ",".join(fields)}
299+
)
300+
301+
assert resp.status_code == 200
302+
303+
resp_json = resp.json()
304+
features = resp_json["features"]
305+
306+
assert len(features) > 0
307+
# These fields are always included in items
308+
constant_fields = ["id", "links"]
309+
if not app.state.settings.use_api_hydrate:
310+
constant_fields.append("collection")
311+
for item in features:
312+
assert set(item.keys()) == set(fields + constant_fields)
313+
314+
274315
async def test_app_sort_extension(load_test_data, app_client, load_test_collection):
275316
coll = load_test_collection
276317
first_item = load_test_data("test_item.json")

0 commit comments

Comments
 (0)