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 2164af1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@
- Include collection assets in `make_all_asset_hrefs_relative/absolute` ([#1168](https://github.com/stac-utils/pystac/pull/1168))
- Use cassettes for all tests that pull files from remote ([#1162](https://github.com/stac-utils/pystac/pull/1162))

### Fixed

- Include the item's root when resolving its collection link ([#1171](https://github.com/stac-utils/pystac/pull/1171))

### Deprecated

- `pystac.summaries.FIELDS_JSON_URL` ([#1045](https://github.com/stac-utils/pystac/pull/1045))
- Catalog `get_item()`. Use `get_items(id)` instead ([#1075](https://github.com/stac-utils/pystac/pull/1075))
- Catalog and Collection `get_all_items`. Use `get_items(recursive=True)` instead ([#1075](https://github.com/stac-utils/pystac/pull/1075))

### Fixed


## [v1.7.3]

### Fixed
Expand Down
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 2164af1

Please sign in to comment.