Skip to content

Fix item field ordering on save_object #1423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ repos:

- id: ruff-format
name: ruff-format
entry: ruff format --force-exclude --check
entry: ruff format --force-exclude
language: system
stages: [commit]
types_or: [python, pyi, jupyter]
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

### Changed

Expand Down
14 changes: 7 additions & 7 deletions pystac/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,25 +365,25 @@ def to_dict(
d: dict[str, Any] = {
"type": "Feature",
"stac_version": pystac.get_stac_version(),
"stac_extensions": self.stac_extensions if self.stac_extensions else [],
"id": self.id,
"properties": self.properties,
"geometry": self.geometry,
"bbox": self.bbox if self.bbox is not None else [],
"properties": self.properties,
"links": [link.to_dict(transform_href=transform_hrefs) for link in links],
"assets": assets,
}

if self.bbox is not None:
d["bbox"] = self.bbox

if self.stac_extensions is not None:
d["stac_extensions"] = self.stac_extensions

if self.collection_id:
d["collection"] = self.collection_id

for key in self.extra_fields:
d[key] = self.extra_fields[key]

# This field is prohibited if there's no geometry
if not self.geometry:
d.pop("bbox")

return d

def clone(self) -> Item:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,30 @@ def test_asset_absolute_href_no_item_self(self) -> None:
actual_href = rel_asset.get_absolute_href()
self.assertEqual(None, actual_href)

def test_item_field_order(self) -> None:
item = pystac.Item.from_file(
TestCases.get_path("data-files/item/sample-item.json")
)
item_dict = item.to_dict(include_self_link=False)
expected_order = [
"type",
"stac_version",
"stac_extensions",
"id",
"geometry",
"bbox",
"properties",
"links",
"assets",
"collection",
]
actual_order = list(item_dict.keys())
self.assertEqual(
actual_order,
expected_order,
f"Order was {actual_order}, expected {expected_order}",
)

def test_extra_fields(self) -> None:
item = pystac.Item.from_file(
TestCases.get_path("data-files/item/sample-item.json")
Expand Down