Skip to content
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

Create valid dict without root href #896

Merged
merged 10 commits into from
Jan 10, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

### Fixed

- Creating dictionaries from Catalogs and Collections without root hrefs now creates valid STAC ([#896](https://github.com/stac-utils/pystac/pull/896))
- 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))

Expand Down
6 changes: 5 additions & 1 deletion pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,11 @@ def get_item_links(self) -> List[Link]:
def to_dict(
self, include_self_link: bool = True, transform_hrefs: bool = True
) -> Dict[str, Any]:
links = self.links
links = [
x
for x in self.links
if x.rel != pystac.RelType.ROOT or x.get_href(transform_hrefs) is not None
]
if not include_self_link:
links = [x for x in links if x.rel != pystac.RelType.SELF]

Expand Down
5 changes: 5 additions & 0 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,11 @@ def test_get_links_media_type(self) -> None:
len(catalog.get_links("search", media_type="application/geo+json")), 1
)

def test_to_dict_no_self_href(self) -> None:
catalog = Catalog(id="an-id", description="A test Catalog")
d = catalog.to_dict(include_self_link=False)
Catalog.from_dict(d)


class FullCopyTest(unittest.TestCase):
def check_link(self, link: pystac.Link, tag: str) -> None:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,16 @@ def test_clone_preserves_assets(self) -> None:
with self.subTest(f"Sets owner for {key}"):
self.assertIs(cloned_asset.owner, cloned_collection)

def test_to_dict_no_self_href(self) -> None:
temporal_extent = TemporalExtent(intervals=[[TEST_DATETIME, None]])
spatial_extent = SpatialExtent(bboxes=ARBITRARY_BBOX)
extent = Extent(spatial=spatial_extent, temporal=temporal_extent)
collection = Collection(
id="an-id", description="A test Collection", extent=extent
)
d = collection.to_dict(include_self_link=False)
Collection.from_dict(d)


class ExtentTest(unittest.TestCase):
def setUp(self) -> None:
Expand Down