Skip to content

fix: preserve empty list/dict values in serialize() when fields specified (#146)#154

Open
rtmalikian wants to merge 1 commit into
beda-software:masterfrom
rtmalikian:fix/empty-list-update-fields
Open

fix: preserve empty list/dict values in serialize() when fields specified (#146)#154
rtmalikian wants to merge 1 commit into
beda-software:masterfrom
rtmalikian:fix/empty-list-update-fields

Conversation

@rtmalikian

Copy link
Copy Markdown

Summary

Fixes a regression in v2.0.6 where save(update_fields=[...]) fails with KeyError when one of the specified fields has an empty list ([]) or empty dict ({}) value.

Root cause: serialize() always calls clean_empty_values(), which strips empty lists and dicts from the output. When save() then tries to extract the specified fields with data = {key: data[key] for key in fields}, the missing key raises KeyError.

Fix: serialize() now accepts an optional fields parameter. When provided, empty values for those specific keys are preserved after cleaning. This ensures that users can intentionally set fields to [] or {} to clear them via a partial update, without breaking the serialization pipeline for other fields.

Changes

File Change
fhirpy/base/resource.py Add fields parameter to serialize(); restore empty values for specified fields after clean_empty_values()
fhirpy/base/lib_async.py Pass fields to serialize() in AsyncClient.save()
fhirpy/base/lib_sync.py Pass fields to serialize() in SyncClient.save()
tests/test_utils.py Add regression tests for empty list and empty dict preservation

Verification

tests/test_utils.py — 12 passed
tests/test_searchset.py — 50 passed
tests/test_lib_base.py — 50 passed
Total: 112 passed, 0 failed

Reproduction (before fix)

resource = client.resource("Patient", generalPractitioner=[practitioner1])
await resource.save()

resource["generalPractitioner"] = []
await resource.save(update_fields=["generalPractitioner"])
# KeyError: 'generalPractitioner' — empty list was stripped by clean_empty_values

After fix, the empty list is preserved for the specified field and the save succeeds.

Fixes #146


About the Author: Raphael Malikian — Clinical AI Solutions Architect. I specialise in building and fixing AI/ML systems for healthcare, including vector databases, RAG pipelines, and clinical NLP. If you need help with your project or think I can add value to your organisation, feel free to reach out — I'd love to connect.

📧 rtmalikian@gmail.com
🔗 GitHub: https://github.com/rtmalikian
🔗 LinkedIn: http://www.linkedin.com/in/raphael-t-malikian-mbbs-bsc-hons-71075436a


Disclosure: This code was developed with assistance from mimo-2.5-pro (Xiaomi) via Hermes Agent (Nous Research). All changes were reviewed, tested against the actual codebase, and verified for correctness.

…fied

When save(update_fields=[...]) is used with a field set to [] or {},
clean_empty_values() strips it from the serialized output, causing
a KeyError when save() tries to extract the specified fields.

Fix by adding a  parameter to serialize() that restores
empty values for explicitly requested fields after cleaning.

Fixes beda-software#146
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Impossible to save resource with update_fields with empty list values

1 participant