Skip to content

Commit e0768a8

Browse files
authored
Merge pull request #291 from stac-utils/fix/rde/item-set-self-href-none
Don't fail on item.set_self_href(None) for items with relative asset hrefs
2 parents 5a492ca + 2f42bc0 commit e0768a8

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Fixed
88

99
- Fixed error when accessing the statistics attribute of the pointcloud extension when no statistics were defined ([#282](https://github.com/stac-utils/pystac/pull/282))
10+
- Fixed exception being thrown when calling set_self_href on items with assets that have relative hrefs ([#291](https://github.com/stac-utils/pystac/pull/291))
1011

1112
### Changed
1213

pystac/item.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def set_self_href(self, href):
127127
super().set_self_href(href)
128128
new_href = self.get_self_href() # May have been made absolute.
129129

130-
if prev_href is not None:
130+
if prev_href is not None and new_href is not None:
131131
# Make sure relative asset links remain valid.
132132
for asset in self.assets.values():
133133
asset_href = asset.href

tests/test_item.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,24 @@ def test_to_from_dict(self):
3737
self.assertEqual(len(item.assets['thumbnail'].properties), 0)
3838

3939
def test_set_self_href_doesnt_break_asset_hrefs(self):
40-
cat = TestCases.test_case_6()
40+
cat = TestCases.test_case_2()
4141
for item in cat.get_all_items():
4242
for asset in item.assets.values():
43-
print(asset.href)
44-
assert not is_absolute_href(asset.href)
43+
if is_absolute_href(asset.href):
44+
asset.href = (f'./{os.path.basename(asset.href)}')
4545
item.set_self_href('http://example.com/item.json')
4646
for asset in item.assets.values():
4747
self.assertTrue(is_absolute_href(asset.href))
48-
self.assertTrue(os.path.exists(asset.href))
48+
49+
def test_set_self_href_none_ignores_relative_asset_hrefs(self):
50+
cat = TestCases.test_case_2()
51+
for item in cat.get_all_items():
52+
for asset in item.assets.values():
53+
if is_absolute_href(asset.href):
54+
asset.href = (f'./{os.path.basename(asset.href)}')
55+
item.set_self_href(None)
56+
for asset in item.assets.values():
57+
self.assertFalse(is_absolute_href(asset.href))
4958

5059
def test_asset_absolute_href(self):
5160
item_dict = self.get_example_item_dict()

0 commit comments

Comments
 (0)