Skip to content

Commit 114b1a1

Browse files
committed
fix: clearing assets in collections
I'm generally uneasy with the way that the base catalog `extra_fields` holds values that might be mutated on the collection. I think this is an argument _for_ #296, namely that while the inheritance makes some things easier, it also causes problems.
1 parent fb0e23f commit 114b1a1

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

pystac/collection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ def to_dict(
569569
d["summaries"] = self.summaries.to_dict()
570570
if any(self.assets):
571571
d["assets"] = {k: v.to_dict() for k, v in self.assets.items()}
572+
elif "assets" in d:
573+
del d["assets"]
572574

573575
return d
574576

tests/test_collection.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ def test_assets(self) -> None:
226226
collection = pystac.Collection.from_dict(data)
227227
collection.validate()
228228

229+
def test_clearing_assets_then_to_dict(self) -> None:
230+
path = TestCases.get_path("data-files/collections/with-assets.json")
231+
collection = pystac.Collection.from_file(path)
232+
assert collection.assets
233+
collection.assets.clear()
234+
assert not collection.assets
235+
collection_as_dict = collection.to_dict()
236+
assert "assets" not in collection_as_dict
237+
229238
def test_from_dict_preserves_dict(self) -> None:
230239
path = TestCases.get_path("data-files/collections/with-assets.json")
231240
with open(path) as f:

0 commit comments

Comments
 (0)