From 415593909e843a34548479ea2d3c0dbd6eee76c5 Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Fri, 23 Jun 2023 09:48:17 -0600 Subject: [PATCH] fix: collection link should be resolved with root Motivated by https://github.com/stac-utils/pystac-client/issues/548 --- pystac/item.py | 4 +++- tests/test_item.py | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pystac/item.py b/pystac/item.py index 05837e20f..4ef676b7f 100644 --- a/pystac/item.py +++ b/pystac/item.py @@ -349,7 +349,9 @@ def get_collection(self) -> Optional[Collection]: if collection_link is None: return None else: - return cast(Collection, collection_link.resolve_stac_object().target) + return cast( + Collection, collection_link.resolve_stac_object(self.get_root()).target + ) def add_derived_from(self, *items: Union[Item, str]) -> Item: """Add one or more items that this is derived from. diff --git a/tests/test_item.py b/tests/test_item.py index cc7ca2b9b..82fe681fe 100644 --- a/tests/test_item.py +++ b/tests/test_item.py @@ -13,7 +13,7 @@ import pystac import pystac.serialization.common_properties -from pystac import Asset, Catalog, Item +from pystac import Asset, Catalog, Collection, Item, Link from pystac.utils import ( datetime_to_str, get_opt, @@ -586,3 +586,24 @@ def test_delete_asset_relative_no_self_link_fails(tmp_asset: pystac.Asset) -> No assert asset.href in str(e.value) assert name in item.assets assert os.path.exists(href) + + +def test_resolve_collection_with_root( + tmp_path: Path, item: Item, collection: Collection +) -> None: + # Motivated by https://github.com/stac-utils/pystac-client/issues/548 + catalog = Catalog("root", "the description") + item.set_root(catalog) + + collection_path = str(tmp_path / "collection.json") + collection.save_object( + include_self_link=False, + dest_href=collection_path, + ) + item.add_link(Link(rel="collection", target=collection_path)) + + read_collection = item.get_collection() + assert read_collection + root = read_collection.get_root() + assert root + assert root.id == "root"