Forward exclude_defaults when jsonable_encoder recurses into dict values#15676
Open
vidigoat wants to merge 1 commit into
Open
Forward exclude_defaults when jsonable_encoder recurses into dict values#15676vidigoat wants to merge 1 commit into
vidigoat wants to merge 1 commit into
Conversation
jsonable_encoder passes exclude_defaults to its recursive calls for
list/tuple/set items, but omits it for dict keys and values. As a result
a Pydantic model nested inside a dict keeps its default-valued fields
when exclude_defaults=True, while the same model inside a list correctly
drops them:
jsonable_encoder([m], exclude_defaults=True) -> [{'foo': 'foo'}] # correct
jsonable_encoder({'m': m}, exclude_defaults=True) -> {'m': {'foo': 'foo', ...}} # wrong, defaults kept
The sibling flags exclude_unset and exclude_none are already forwarded in
the same dict branch, so this is an inconsistency, not intended behavior.
Forward exclude_defaults too. Adds a regression test.
Authored with the assistance of an AI tool; reviewed and verified by me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
jsonable_encoderforwardsexclude_defaultsto its recursive calls forlist/tuple/setitems, but omits it when recursing intodictkeys and values. So a Pydantic model nested inside adictkeeps its default-valued fields underexclude_defaults=True, while the same model inside alistcorrectly drops them:The sibling flags
exclude_unsetandexclude_noneare already forwarded in that samedictbranch, so this is a copy/paste inconsistency rather than intended behavior. (include/excludeare intentionally top-level-only and are correctly not forwarded.)Fix
Forward
exclude_defaultsin the two recursivejsonable_encodercalls inside thedictbranch (forencoded_keyandencoded_value), matching how the list/tuple/set branch already does it.Test plan
Added
test_encode_model_with_default_in_containertotests/test_jsonable_encoder.py, asserting a model-in-dict behaves the same as model-in-list underexclude_defaults/exclude_unset.No behavior change for the already-correct list/tuple/set path; only the dict path is brought into line.
Disclosure: authored with the assistance of an AI tool; fully reviewed and verified by me.