Skip to content

Commit 3f5db8d

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

File tree

3 files changed

+1456
-1374
lines changed

3 files changed

+1456
-1374
lines changed

stac_fastapi/core/stac_fastapi/core/serializers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from stac_fastapi.core.datetime_utils import now_to_rfc3339_str
1212
from stac_fastapi.core.models.links import CollectionLinks
13-
from stac_fastapi.core.utilities import get_bool_env
13+
from stac_fastapi.core.utilities import get_bool_env, hide_private_data
1414
from stac_fastapi.types import stac as stac_types
1515
from stac_fastapi.types.links import ItemLinks, resolve_links
1616

@@ -108,7 +108,7 @@ def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item:
108108
else:
109109
assets = item.get("assets", {})
110110

111-
return stac_types.Item(
111+
stac_item = stac_types.Item(
112112
type="Feature",
113113
stac_version=item.get("stac_version", ""),
114114
stac_extensions=item.get("stac_extensions", []),
@@ -121,6 +121,12 @@ def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item:
121121
assets=assets,
122122
)
123123

124+
if get_bool_env("HIDE_PRIVATE_DATA"):
125+
fields_to_remove = ["private_data"]
126+
stac_item = hide_private_data(stac_item, fields_to_remove)
127+
128+
return stac_item
129+
124130

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

stac_fastapi/core/stac_fastapi/core/utilities.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
such as converting bounding boxes to polygon representations.
55
"""
66

7+
import copy
78
import logging
89
import os
910
from typing import Any, Dict, List, Optional, Set, Union
@@ -178,3 +179,34 @@ def dict_deep_update(merge_to: Dict[str, Any], merge_from: Dict[str, Any]) -> No
178179
dict_deep_update(merge_to[k], merge_from[k])
179180
else:
180181
merge_to[k] = v
182+
183+
184+
def hide_private_data(obj: Any, private_field_names: Union[str, List[str]]) -> Any:
185+
"""Hide private data fields from the object.
186+
187+
Args:
188+
obj: The object to process (dict, list, or other)
189+
private_field_names: A list or single field names to hide from response
190+
"""
191+
obj = copy.deepcopy(obj)
192+
193+
field_names = (
194+
[private_field_names]
195+
if isinstance(private_field_names, str)
196+
else private_field_names
197+
)
198+
199+
if not field_names:
200+
return obj
201+
202+
if isinstance(obj, dict):
203+
for field in field_names:
204+
obj.pop(field, None)
205+
for value in obj.values():
206+
hide_private_data(value, field_names)
207+
208+
elif isinstance(obj, list):
209+
for item in obj:
210+
hide_private_data(item, field_names)
211+
212+
return obj

0 commit comments

Comments
 (0)