mirror of https://github.com/tiangolo/fastapi.git
✨ When using Pydantic models with __root__ use the internal value in jsonable_encoder (#1524)
This commit is contained in:
parent
748bedd37c
commit
8fb755703d
|
|
@ -71,6 +71,8 @@ def jsonable_encoder(
|
||||||
by_alias=by_alias,
|
by_alias=by_alias,
|
||||||
skip_defaults=bool(exclude_unset or skip_defaults),
|
skip_defaults=bool(exclude_unset or skip_defaults),
|
||||||
)
|
)
|
||||||
|
if "__root__" in obj_dict:
|
||||||
|
obj_dict = obj_dict["__root__"]
|
||||||
return jsonable_encoder(
|
return jsonable_encoder(
|
||||||
obj_dict,
|
obj_dict,
|
||||||
exclude_none=exclude_none,
|
exclude_none=exclude_none,
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,10 @@ class ModelWithDefault(BaseModel):
|
||||||
bla: str = "bla"
|
bla: str = "bla"
|
||||||
|
|
||||||
|
|
||||||
|
class ModelWithRoot(BaseModel):
|
||||||
|
__root__: str
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(
|
@pytest.fixture(
|
||||||
name="model_with_path", params=[PurePath, PurePosixPath, PureWindowsPath]
|
name="model_with_path", params=[PurePath, PurePosixPath, PureWindowsPath]
|
||||||
)
|
)
|
||||||
|
|
@ -158,3 +162,8 @@ def test_encode_model_with_path(model_with_path):
|
||||||
else:
|
else:
|
||||||
expected = "/foo/bar"
|
expected = "/foo/bar"
|
||||||
assert jsonable_encoder(model_with_path) == {"path": expected}
|
assert jsonable_encoder(model_with_path) == {"path": expected}
|
||||||
|
|
||||||
|
|
||||||
|
def test_encode_root():
|
||||||
|
model = ModelWithRoot(__root__="Foo")
|
||||||
|
assert jsonable_encoder(model) == "Foo"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue