Browse Source
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.
pull/15676/head
2 changed files with 15 additions and 0 deletions
Loading…
Reference in new issue