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:
voegtlel
2020-04-05 15:04:46 +02:00
committed by GitHub
parent 766157bfb4
commit 3397d4d69a
6 changed files with 223 additions and 16 deletions

View File

@@ -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