Skip to content

Fix serializing optional Collection attributes #916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
### Fixed

- Dependency resolution when installing `requirements-dev.txt` ([#897](https://github.com/stac-utils/pystac/pull/897))
- Serializing optional Collection attributes ([#916](https://github.com/stac-utils/pystac/pull/916))

## [v1.6.1]

Expand Down
2 changes: 1 addition & 1 deletion pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def to_dict(
"links": [link.to_dict(transform_href=transform_hrefs) for link in links],
}

if self.stac_extensions is not None:
if self.stac_extensions:
d["stac_extensions"] = self.stac_extensions

for key in self.extra_fields:
Expand Down
22 changes: 11 additions & 11 deletions pystac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,11 @@ def to_dict(
)
d["extent"] = self.extent.to_dict()
d["license"] = self.license
if self.stac_extensions is not None:
if self.stac_extensions:
d["stac_extensions"] = self.stac_extensions
if self.keywords is not None:
if self.keywords:
d["keywords"] = self.keywords
if self.providers is not None:
if self.providers:
d["providers"] = list(map(lambda x: x.to_dict(), self.providers))
if not self.summaries.is_empty():
d["summaries"] = self.summaries.to_dict()
Expand Down Expand Up @@ -629,19 +629,19 @@ def from_dict(
description = d.pop("description")
license = d.pop("license")
extent = Extent.from_dict(d.pop("extent"))
title = d.get("title")
stac_extensions = d.get("stac_extensions")
keywords = d.get("keywords")
providers = d.get("providers")
title = d.pop("title", None)
stac_extensions = d.pop("stac_extensions", None)
keywords = d.pop("keywords", None)
providers = d.pop("providers", None)
if providers is not None:
providers = list(map(lambda x: pystac.Provider.from_dict(x), providers))
summaries = d.get("summaries")
summaries = d.pop("summaries", None)
if summaries is not None:
summaries = Summaries(summaries)

assets: Optional[Dict[str, Any]] = {
k: Asset.from_dict(v) for k, v in d.get("assets", {}).items()
}
assets = d.pop("assets", None)
if assets:
assets = {k: Asset.from_dict(v) for k, v in assets.items()}
links = d.pop("links")

d.pop("stac_version")
Expand Down
37 changes: 37 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,43 @@ def test_assets(self) -> None:
collection = pystac.Collection.from_dict(data)
collection.validate()

def test_removing_optional_attributes(self) -> None:
path = TestCases.get_path("data-files/collections/with-assets.json")
with open(path, "r") as file:
data = json.load(file)
data["title"] = "dummy title"
data["stac_extensions"] = ["dummy extension"]
data["keywords"] = ["key", "word"]
data["providers"] = [{"name": "pystac"}]
collection = pystac.Collection.from_dict(data)

# Assert we have everything set
assert collection.title
assert collection.stac_extensions
assert collection.keywords
assert collection.providers
assert collection.summaries
assert collection.assets

# Remove all of the optional stuff
collection.title = None
collection.stac_extensions = []
collection.keywords = []
collection.providers = []
collection.summaries = pystac.Summaries({})
collection.assets = {}

collection_as_dict = collection.to_dict()
for key in (
"title",
"stac_extensions",
"keywords",
"providers",
"summaries",
"assets",
):
assert key not in collection_as_dict

def test_from_dict_preserves_dict(self) -> None:
path = TestCases.get_path("data-files/collections/with-assets.json")
with open(path) as f:
Expand Down