Skip to content

Commit 7ccc7df

Browse files
authored
Fix item field ordering on save_object (#1423)
* Fix `Item` field ordering * Simplify ordering test
1 parent 72deee6 commit 7ccc7df

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ repos:
2020

2121
- id: ruff-format
2222
name: ruff-format
23-
entry: ruff format --force-exclude --check
23+
entry: ruff format --force-exclude
2424
language: system
2525
stages: [commit]
2626
types_or: [python, pyi, jupyter]

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Add netCDF to pystac.media_type ([#1386](https://github.com/stac-utils/pystac/pull/1386))
88
- Add convenience method for accessing pystac_client ([#1365](https://github.com/stac-utils/pystac/pull/1365))
9+
- Fix field ordering when saving `Item`s ([#1423](https://github.com/stac-utils/pystac/pull/1423))
910

1011
### Changed
1112

pystac/item.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,25 +365,25 @@ def to_dict(
365365
d: dict[str, Any] = {
366366
"type": "Feature",
367367
"stac_version": pystac.get_stac_version(),
368+
"stac_extensions": self.stac_extensions if self.stac_extensions else [],
368369
"id": self.id,
369-
"properties": self.properties,
370370
"geometry": self.geometry,
371+
"bbox": self.bbox if self.bbox is not None else [],
372+
"properties": self.properties,
371373
"links": [link.to_dict(transform_href=transform_hrefs) for link in links],
372374
"assets": assets,
373375
}
374376

375-
if self.bbox is not None:
376-
d["bbox"] = self.bbox
377-
378-
if self.stac_extensions is not None:
379-
d["stac_extensions"] = self.stac_extensions
380-
381377
if self.collection_id:
382378
d["collection"] = self.collection_id
383379

384380
for key in self.extra_fields:
385381
d[key] = self.extra_fields[key]
386382

383+
# This field is prohibited if there's no geometry
384+
if not self.geometry:
385+
d.pop("bbox")
386+
387387
return d
388388

389389
def clone(self) -> Item:

tests/test_item.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,30 @@ def test_asset_absolute_href_no_item_self(self) -> None:
108108
actual_href = rel_asset.get_absolute_href()
109109
self.assertEqual(None, actual_href)
110110

111+
def test_item_field_order(self) -> None:
112+
item = pystac.Item.from_file(
113+
TestCases.get_path("data-files/item/sample-item.json")
114+
)
115+
item_dict = item.to_dict(include_self_link=False)
116+
expected_order = [
117+
"type",
118+
"stac_version",
119+
"stac_extensions",
120+
"id",
121+
"geometry",
122+
"bbox",
123+
"properties",
124+
"links",
125+
"assets",
126+
"collection",
127+
]
128+
actual_order = list(item_dict.keys())
129+
self.assertEqual(
130+
actual_order,
131+
expected_order,
132+
f"Order was {actual_order}, expected {expected_order}",
133+
)
134+
111135
def test_extra_fields(self) -> None:
112136
item = pystac.Item.from_file(
113137
TestCases.get_path("data-files/item/sample-item.json")

0 commit comments

Comments
 (0)