Skip to content

Commit

Permalink
fix: collection link should be resolved with root
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Jun 23, 2023
1 parent 568c3af commit 4155939
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pystac/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 22 additions & 1 deletion tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"

0 comments on commit 4155939

Please sign in to comment.