Skip to content

Fix serializing optional Collection attributes #916

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 4 commits into from
Dec 30, 2022
Merged

Fix serializing optional Collection attributes #916

merged 4 commits into from
Dec 30, 2022

Conversation

gadomski
Copy link
Member

@gadomski gadomski commented Nov 9, 2022

Description:

The underlying problem is that, when creating a Collection from a dict, some values are pulled out to Collection-level attributes, e.g.

pystac/pystac/collection.py

Lines 644 to 646 in 114b1a1

assets: Optional[Dict[str, Any]] = {
k: Asset.from_dict(v) for k, v in d.get("assets", {}).items()
}

However, those values are not removed from the original dictionary d, and that original dictionary is used to populate extra_fields:

extra_fields=d,

This means that the assets attribute of a Collection isn't the only place where assets (and other attributes) are stored, and in fact when a collection is turned back into a dictionary, those assets are re-populated from extra_fields:

pystac/pystac/catalog.py

Lines 516 to 517 in 114b1a1

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

This fixes the issue by popping these optional attributes out of the source dictionary, and by checking for these attributes' presence in a better way (i.e. before we were checking if lists were not None 😞).

PR Checklist:

  • Code is formatted (run pre-commit run --all-files)
  • Tests pass (run scripts/test)
  • Documentation has been updated to reflect changes, if applicable
  • This PR maintains or improves overall codebase code coverage.
  • Changes are added to the CHANGELOG. See the docs for information about adding to the changelog.

@gadomski gadomski requested a review from philvarner November 9, 2022 21:06
@codecov-commenter
Copy link

codecov-commenter commented Nov 9, 2022

Codecov Report

Base: 94.34% // Head: 94.37% // Increases project coverage by +0.02% 🎉

Coverage data is based on head (80aa478) compared to base (e8da685).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #916      +/-   ##
==========================================
+ Coverage   94.34%   94.37%   +0.02%     
==========================================
  Files          83       83              
  Lines       11989    12015      +26     
  Branches     1134     1136       +2     
==========================================
+ Hits        11311    11339      +28     
  Misses        496      496              
+ Partials      182      180       -2     
Impacted Files Coverage Δ
pystac/catalog.py 91.58% <100.00%> (+0.24%) ⬆️
pystac/collection.py 92.88% <100.00%> (+0.45%) ⬆️
tests/test_collection.py 99.60% <100.00%> (+0.04%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@gadomski gadomski added the bugfix Fixes a bug label Nov 10, 2022
@gadomski gadomski self-assigned this Dec 30, 2022
@gadomski gadomski merged commit 155cabe into main Dec 30, 2022
@gadomski gadomski deleted the clear-assets branch December 30, 2022 18:01
@gadomski gadomski added this to the 1.7 milestone Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants