Skip to content

Commit 7f8e7aa

Browse files
author
Jon Duckworth
authored
Merge pull request #419 from duckontheweb/fix/369-idempotent-add-to
Make ExtensionManagementMixin.add_to idempotent
2 parents 12866c4 + 77b5d97 commit 7f8e7aa

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
- `EOExtension.get_bands` returns `None` for asset without EO bands ([#406](https://github.com/stac-utils/pystac/pull/406))
2727
- `identify_stac_object_type` returns `None` and `identify_stac_object` raises `STACTypeError` for non-STAC objects
2828
([#402](https://github.com/stac-utils/pystac/pull/402))
29+
- `ExtensionManagementMixin.add_to` is now idempotent (only adds schema URI to
30+
`stac_extensions` once per `Item` regardless of the number of calls) ([#419](https://github.com/stac-utils/pystac/pull/419))
2931

3032
### Removed
3133

pystac/extensions/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ def get_schema_uri(cls) -> str:
100100
@classmethod
101101
def add_to(cls, obj: S) -> None:
102102
"""Add the schema URI for this extension to the
103-
:attr:`pystac.STACObject.stac_extensions` list for the given object."""
103+
:attr:`~pystac.STACObject.stac_extensions` list for the given object, if it is
104+
not already present."""
104105
if obj.stac_extensions is None:
105106
obj.stac_extensions = [cls.get_schema_uri()]
106-
else:
107+
elif cls.get_schema_uri() not in obj.stac_extensions:
107108
obj.stac_extensions.append(cls.get_schema_uri())
108109

109110
@classmethod

tests/extensions/test_eo.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class EOTest(unittest.TestCase):
4343
)
4444
EO_COLLECTION_URI = TestCases.get_path("data-files/eo/eo-collection.json")
4545
S2_ITEM_URI = TestCases.get_path("data-files/eo/eo-sentinel2-item.json")
46+
PLAIN_ITEM = TestCases.get_path("data-files/item/sample-item.json")
4647

4748
def setUp(self) -> None:
4849
self.maxDiff = None
@@ -52,6 +53,24 @@ def test_to_from_dict(self) -> None:
5253
item_dict = json.load(f)
5354
assert_to_from_dict(self, Item, item_dict)
5455

56+
def test_add_to(self) -> None:
57+
item = Item.from_file(self.PLAIN_ITEM)
58+
self.assertNotIn(EOExtension.get_schema_uri(), item.stac_extensions)
59+
60+
# Check that the URI gets added to stac_extensions
61+
EOExtension.add_to(item)
62+
self.assertIn(EOExtension.get_schema_uri(), item.stac_extensions)
63+
64+
# Check that the URI only gets added once, regardless of how many times add_to
65+
# is called.
66+
EOExtension.add_to(item)
67+
EOExtension.add_to(item)
68+
69+
eo_uris = [
70+
uri for uri in item.stac_extensions if uri == EOExtension.get_schema_uri()
71+
]
72+
self.assertEqual(len(eo_uris), 1)
73+
5574
def test_validate_eo(self) -> None:
5675
item = pystac.Item.from_file(self.LANDSAT_EXAMPLE_URI)
5776
item2 = pystac.Item.from_file(self.BANDS_IN_ITEM_URI)

0 commit comments

Comments
 (0)