Skip to content

Commit e0f53e9

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
fix: Adding a key for technical/private data in SFEOS
1 parent f834a9e commit e0f53e9

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

stac_fastapi/core/stac_fastapi/core/serializers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import abc
44
import logging
5+
import os
56
from copy import deepcopy
67
from typing import Any, List, Optional
78

@@ -108,7 +109,7 @@ def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item:
108109
else:
109110
assets = item.get("assets", {})
110111

111-
return stac_types.Item(
112+
stac_item = stac_types.Item(
112113
type="Feature",
113114
stac_version=item.get("stac_version", ""),
114115
stac_extensions=item.get("stac_extensions", []),
@@ -121,6 +122,11 @@ def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item:
121122
assets=assets,
122123
)
123124

125+
private_data_field = os.getenv("PRIVATE_DATA_FIELD")
126+
if get_bool_env("HIDE_PRIVATE_DATA") and private_data_field:
127+
stac_item.pop(private_data_field, None)
128+
return stac_item
129+
124130

125131
class CollectionSerializer(Serializer):
126132
"""Serialization methods for STAC collections."""

stac_fastapi/tests/api/test_api.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,3 +1657,55 @@ async def test_use_datetime_false(app_client, load_test_data, txn_client, monkey
16571657

16581658
assert "test-item-datetime-only" not in found_ids
16591659
assert "test-item-start-end-only" in found_ids
1660+
1661+
1662+
@pytest.mark.asyncio
1663+
async def test_hide_private_data_true(app_client, txn_client, load_test_data):
1664+
"""Test hiding the private data from response"""
1665+
1666+
os.environ["PRIVATE_DATA_FIELD"] = "private_field"
1667+
os.environ["HIDE_PRIVATE_DATA"] = "true"
1668+
1669+
test_collection = load_test_data("test_collection.json")
1670+
test_collection_id = "test-collection-hide-private-data"
1671+
test_collection["id"] = test_collection_id
1672+
await create_collection(txn_client, test_collection)
1673+
1674+
test_item = load_test_data("test_item.json")
1675+
test_item_id = "test-item-hide-private-data"
1676+
test_item["id"] = test_item_id
1677+
test_item["collection"] = test_collection_id
1678+
test_item["private_data"] = {"item_secret": "sensitive_info"}
1679+
await create_item(txn_client, test_item)
1680+
1681+
# Test /collections/{collection_id}/items
1682+
resp = await app_client.get(f"/collections/{test_collection_id}/items")
1683+
assert resp.status_code == 200
1684+
resp_json = resp.json()
1685+
item = resp_json["features"][0]
1686+
assert "private_data" not in item
1687+
1688+
# Test /collections/{collection_id}/items/{item_id}
1689+
resp = await app_client.get(
1690+
f"/collections/{test_collection_id}/items/{test_item_id}"
1691+
)
1692+
assert resp.status_code == 200
1693+
resp_json = resp.json()
1694+
assert "private_data" not in resp_json
1695+
1696+
# Test GET /search
1697+
resp = await app_client.get(f"/search?collections={test_collection_id}")
1698+
assert resp.status_code == 200
1699+
resp_json = resp.json()
1700+
item = resp_json["features"][0]
1701+
assert "private_data" not in item
1702+
1703+
# Test POST /search
1704+
resp = await app_client.post("/search", json={"collections": [test_collection_id]})
1705+
assert resp.status_code == 200
1706+
resp_json = resp.json()
1707+
item = resp_json["features"][0]
1708+
assert "private_data" not in item
1709+
1710+
del os.environ["PRIVATE_DATA_FIELD"]
1711+
del os.environ["HIDE_PRIVATE_DATA"]

0 commit comments

Comments
 (0)