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

Remove duplicate self links on items #1103

Merged
merged 1 commit into from
Apr 25, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Duplicate `self` links in Items ([#1103](https://github.com/stac-utils/pystac/pull/1103))

## [v1.7.2]

### Fixed
Expand Down
10 changes: 3 additions & 7 deletions pystac/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, TypeVar, Union, cast

import pystac
from pystac import STACError, STACObjectType
from pystac import RelType, STACError, STACObjectType
from pystac.asset import Asset
from pystac.catalog import Catalog
from pystac.collection import Collection
Expand Down Expand Up @@ -476,13 +476,9 @@ def from_dict(
assets={k: Asset.from_dict(v) for k, v in assets.items()},
)

has_self_link = False
for link in links:
has_self_link |= link["rel"] == pystac.RelType.SELF
item.add_link(Link.from_dict(link))

if not has_self_link and href is not None:
item.add_link(Link.self_href(href))
if href is None or link.get("rel", None) != RelType.SELF:
item.add_link(Link.from_dict(link))

if root:
item.set_root(root)
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ def test_case_1_catalog() -> Catalog:
def projection_landsat8_item() -> Item:
path = TestCases.get_path("data-files/projection/example-landsat8.json")
return Item.from_file(path)


@pytest.fixture
def sample_item() -> Item:
return Item.from_file(TestCases.get_path("data-files/item/sample-item.json"))
10 changes: 10 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tempfile
import unittest
from copy import deepcopy
from pathlib import Path
from typing import Any, Dict, Optional

import dateutil.relativedelta
Expand Down Expand Up @@ -468,3 +469,12 @@ def test_geo_interface() -> None:
item.to_dict(include_self_link=False, transform_hrefs=False)
== item.__geo_interface__
)


def test_duplicate_self_links(tmp_path: Path, sample_item: pystac.Item) -> None:
# https://github.com/stac-utils/pystac/issues/1102
assert len(sample_item.get_links(rel="self")) == 1
path = tmp_path / "item.json"
sample_item.save_object(include_self_link=True, dest_href=str(path))
sample_item = Item.from_file(str(path))
assert len(sample_item.get_links(rel="self")) == 1