Skip to content

Commit de8c63c

Browse files
committed
Merge branch 'main' into link-media-type
2 parents f5ad1a7 + c033b51 commit de8c63c

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Allow links to have `file://` prefix - but don't write them that way by default ([#1489](https://github.com/stac-utils/pystac/pull/1489))
2020
- For `get_root_link`, `get_child_links`, `get_item_links`: Ensure json media types ([#1497](https://github.com/stac-utils/pystac/pull/1497))
2121
- Raise `STACError` with message when a link is expected to resolve to a STAC object but doesn't ([#1500](https://github.com/stac-utils/pystac/pull/1500))
22+
- Raise an error on APILayoutStrategy when root_href is non-url ([#1498](https://github.com/stac-utils/pystac/pull/1498))
2223

2324
### Fixed
2425

pystac/catalog.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
import pystac
1616
import pystac.media_type
1717
from pystac.cache import ResolvedObjectCache
18-
from pystac.errors import STACTypeError
18+
from pystac.errors import STACError, STACTypeError
1919
from pystac.layout import (
20+
APILayoutStrategy,
2021
BestPracticesLayoutStrategy,
2122
HrefLayoutStrategy,
2223
LayoutTemplate,
@@ -31,6 +32,7 @@
3132
from pystac.utils import (
3233
HREF,
3334
StringEnum,
35+
_is_url,
3436
is_absolute_href,
3537
make_absolute_href,
3638
make_relative_href,
@@ -775,6 +777,9 @@ def normalize_hrefs(
775777
if not is_absolute_href(root_href):
776778
root_href = make_absolute_href(root_href, os.getcwd(), start_is_dir=True)
777779

780+
if isinstance(_strategy, APILayoutStrategy) and not _is_url(root_href):
781+
raise STACError("When using APILayoutStrategy the root_href must be a URL")
782+
778783
def process_item(
779784
item: Item, _root_href: str, is_root: bool, parent: Catalog | None
780785
) -> Callable[[], None] | None:

pystac/stac_io.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
merge_common_properties,
1717
migrate_to_latest,
1818
)
19-
from pystac.utils import HREF, safe_urlparse
19+
from pystac.utils import HREF, _is_url, safe_urlparse
2020

2121
# Use orjson if available
2222
try:
@@ -391,11 +391,6 @@ def _report_duplicate_object_names(
391391
return result
392392

393393

394-
def _is_url(href: str) -> bool:
395-
parsed = safe_urlparse(href)
396-
return parsed.scheme not in ["", "file"]
397-
398-
399394
if HAS_URLLIB3:
400395
from typing import cast
401396

pystac/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,9 @@ def is_file_path(href: str) -> bool:
606606
"""
607607
parsed = urlparse(href)
608608
return bool(os.path.splitext(parsed.path)[1])
609+
610+
611+
def _is_url(href: str) -> bool:
612+
"""Checks if an HREF is a url rather than a local path"""
613+
parsed = safe_urlparse(href)
614+
return parsed.scheme not in ["", "file"]

tests/test_catalog.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
)
2727
from pystac.errors import STACError
2828
from pystac.layout import (
29+
APILayoutStrategy,
2930
BestPracticesLayoutStrategy,
3031
HrefLayoutStrategy,
3132
TemplateLayoutStrategy,
@@ -1975,6 +1976,18 @@ def test_add_item_layout_strategy(
19751976
assert item.self_href == f"{base_url}/{template}/{item_id}.json"
19761977

19771978

1979+
def test_APILayoutStrategy_requires_root_to_be_url(
1980+
catalog: Catalog, collection: Collection, item: Item
1981+
) -> None:
1982+
collection.add_item(item)
1983+
catalog.add_child(collection)
1984+
with pytest.raises(
1985+
pystac.errors.STACError,
1986+
match="When using APILayoutStrategy the root_href must be a URL",
1987+
):
1988+
catalog.normalize_hrefs(root_href="issues-1486", strategy=APILayoutStrategy())
1989+
1990+
19781991
def test_get_child_links_cares_about_media_type(catalog: pystac.Catalog) -> None:
19791992
catalog.links.extend(
19801993
[

0 commit comments

Comments
 (0)