Skip to content

Commit c033b51

Browse files
authored
Raise an error on APILayoutStrategy when root_href is non-url (#1498)
* Raise an error on APILayoutStrategy when root_href is non-url * Make `utils._is_url` private
1 parent 67931b5 commit c033b51

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
@@ -17,6 +17,7 @@
1717
- Update migrate code to handle license changes in STAC spec 1.1.0 ([#1491](https://github.com/stac-utils/pystac/pull/1491))
1818
- Allow links to have `file://` prefix - but don't write them that way by default ([#1489](https://github.com/stac-utils/pystac/pull/1489))
1919
- 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))
20+
- Raise an error on APILayoutStrategy when root_href is non-url ([#1498](https://github.com/stac-utils/pystac/pull/1498))
2021

2122
### Fixed
2223

pystac/catalog.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
import pystac
1616
from pystac.cache import ResolvedObjectCache
17-
from pystac.errors import STACTypeError
17+
from pystac.errors import STACError, STACTypeError
1818
from pystac.layout import (
19+
APILayoutStrategy,
1920
BestPracticesLayoutStrategy,
2021
HrefLayoutStrategy,
2122
LayoutTemplate,
@@ -30,6 +31,7 @@
3031
from pystac.utils import (
3132
HREF,
3233
StringEnum,
34+
_is_url,
3335
is_absolute_href,
3436
make_absolute_href,
3537
make_relative_href,
@@ -769,6 +771,9 @@ def normalize_hrefs(
769771
if not is_absolute_href(root_href):
770772
root_href = make_absolute_href(root_href, os.getcwd(), start_is_dir=True)
771773

774+
if isinstance(_strategy, APILayoutStrategy) and not _is_url(root_href):
775+
raise STACError("When using APILayoutStrategy the root_href must be a URL")
776+
772777
def process_item(
773778
item: Item, _root_href: str, is_root: bool, parent: Catalog | None
774779
) -> 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,
@@ -1969,3 +1970,15 @@ def test_add_item_layout_strategy(
19691970
template = template.format(id=item.id).replace("$", "")
19701971

19711972
assert item.self_href == f"{base_url}/{template}/{item_id}.json"
1973+
1974+
1975+
def test_APILayoutStrategy_requires_root_to_be_url(
1976+
catalog: Catalog, collection: Collection, item: Item
1977+
) -> None:
1978+
collection.add_item(item)
1979+
catalog.add_child(collection)
1980+
with pytest.raises(
1981+
pystac.errors.STACError,
1982+
match="When using APILayoutStrategy the root_href must be a URL",
1983+
):
1984+
catalog.normalize_hrefs(root_href="issues-1486", strategy=APILayoutStrategy())

0 commit comments

Comments
 (0)