From 33ab4c3cd2e4ee924c4406292ae869e8472a8b04 Mon Sep 17 00:00:00 2001 From: Julia Signell Date: Mon, 1 May 2023 14:46:13 -0400 Subject: [PATCH] Allow nullable `stac_extensions` --- CHANGELOG.md | 1 + pystac/serialization/migrate.py | 11 +++-------- tests/serialization/test_migrate.py | 10 ++++++++++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e16f464b1..63466c868 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Expand support for previous extension schema URIs ([#1091](https://github.com/stac-utils/pystac/pull/1091)) - Use `pyproject.toml` instead of `setup.py` ([#1100](https://github.com/stac-utils/pystac/pull/1100)) - `DefaultStacIO` now raises an error if it tries to write to a non-local url ([#1107](https://github.com/stac-utils/pystac/pull/1107)) +- Allow instantiation of pystac objects even with `"stac_extensions": null` ([#1109](https://github.com/stac-utils/pystac/pull/1109)) ### Deprecated diff --git a/pystac/serialization/migrate.py b/pystac/serialization/migrate.py index ca2744be5..0353979fe 100644 --- a/pystac/serialization/migrate.py +++ b/pystac/serialization/migrate.py @@ -184,15 +184,10 @@ def migrate_to_latest( if version != STACVersion.DEFAULT_STAC_VERSION: object_migrations[info.object_type](result, version, info) - if "stac_extensions" not in result: - # Force stac_extensions property, as it makes - # downstream migration less complex - result["stac_extensions"] = [] result["stac_version"] = STACVersion.DEFAULT_STAC_VERSION - else: - # Ensure stac_extensions property for consistency - if "stac_extensions" not in result: - result["stac_extensions"] = [] + + # Ensure stac_extensions property for consistency + result["stac_extensions"] = result.get("stac_extensions", None) or [] pystac.EXTENSION_HOOKS.migrate(result, version, info) for ext in result["stac_extensions"][:]: diff --git a/tests/serialization/test_migrate.py b/tests/serialization/test_migrate.py index 957f5ecd5..d7fb2f8fd 100644 --- a/tests/serialization/test_migrate.py +++ b/tests/serialization/test_migrate.py @@ -99,3 +99,13 @@ def test_should_raise_exception_when_passing_invalid_extension_object( match=r"^Item Assets extension does not apply to type 'object'$", ): ItemAssetsExtension.ext(object()) # type: ignore + + +def test_migrate_works_even_if_stac_extensions_is_null( + test_case_1_catalog: pystac.Catalog, +) -> None: + collection = list(test_case_1_catalog.get_all_collections())[0] + collection_dict = collection.to_dict() + collection_dict["stac_extensions"] = None + + pystac.Collection.from_dict(collection_dict, migrate=True)