mirror of
https://github.com/fastapi/fastapi.git
synced 2026-04-04 15:13:26 -04:00
✨ Implement response_model_exclude_defaults and response_model_exclude_none (#1166)
* Implemented response_model_exclude_defaults and response_model_exclude_none to be compatible pydantic options. * 🚚 Rename and invert include_none to exclude_none to keep in sync with Pydantic Co-authored-by: Lukas Voegtle <lukas.voegtle@sick.de> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
@@ -70,6 +70,12 @@ class ModelWithAlias(BaseModel):
|
||||
foo: str = Field(..., alias="Foo")
|
||||
|
||||
|
||||
class ModelWithDefault(BaseModel):
|
||||
foo: str = ...
|
||||
bar: str = "bar"
|
||||
bla: str = "bla"
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="model_with_path", params=[PurePath, PurePosixPath, PureWindowsPath]
|
||||
)
|
||||
@@ -121,6 +127,16 @@ def test_encode_model_with_alias():
|
||||
assert jsonable_encoder(model) == {"Foo": "Bar"}
|
||||
|
||||
|
||||
def test_encode_model_with_default():
|
||||
model = ModelWithDefault(foo="foo", bar="bar")
|
||||
assert jsonable_encoder(model) == {"foo": "foo", "bar": "bar", "bla": "bla"}
|
||||
assert jsonable_encoder(model, exclude_unset=True) == {"foo": "foo", "bar": "bar"}
|
||||
assert jsonable_encoder(model, exclude_defaults=True) == {"foo": "foo"}
|
||||
assert jsonable_encoder(model, exclude_unset=True, exclude_defaults=True) == {
|
||||
"foo": "foo"
|
||||
}
|
||||
|
||||
|
||||
def test_custom_encoders():
|
||||
class safe_datetime(datetime):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user