🐛 Fix jsonable_encoder using include and exclude parameters for non-Pydantic objects (#2606)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
Xavi Moreno
2022-08-19 06:48:21 +10:00
committed by GitHub
parent 8a9a117ec7
commit eb2e183361
2 changed files with 42 additions and 1 deletions

View File

@@ -80,6 +80,11 @@ def jsonable_encoder(
return obj
if isinstance(obj, dict):
encoded_dict = {}
allowed_keys = set(obj.keys())
if include is not None:
allowed_keys &= set(include)
if exclude is not None:
allowed_keys -= set(exclude)
for key, value in obj.items():
if (
(
@@ -88,7 +93,7 @@ def jsonable_encoder(
or (not key.startswith("_sa"))
)
and (value is not None or not exclude_none)
and ((include and key in include) or not exclude or key not in exclude)
and key in allowed_keys
):
encoded_key = jsonable_encoder(
key,
@@ -144,6 +149,8 @@ def jsonable_encoder(
raise ValueError(errors)
return jsonable_encoder(
data,
include=include,
exclude=exclude,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,