obj: Any,
include: Set[str] = None,
exclude: Set[str] = set(),
- by_alias: bool = False,
+ by_alias: bool = True,
include_none: bool = True,
custom_encoder: dict = {},
sqlalchemy_safe: bool = True,
import pytest
from fastapi.encoders import jsonable_encoder
-from pydantic import BaseModel
+from pydantic import BaseModel, Schema, ValidationError
class Person:
use_enum_values = True
+class ModelWithAlias(BaseModel):
+ foo: str = Schema(..., alias="Foo")
+
+
def test_encode_class():
person = Person(name="Foo")
pet = Pet(owner=person, name="Firulais")
def test_encode_model_with_config():
model = ModelWithConfig(role=RoleEnum.admin)
assert jsonable_encoder(model) == {"role": "admin"}
+
+
+def test_encode_model_with_alias_raises():
+ with pytest.raises(ValidationError):
+ model = ModelWithAlias(foo="Bar")
+
+
+def test_encode_model_with_alias():
+ model = ModelWithAlias(Foo="Bar")
+ assert jsonable_encoder(model) == {"Foo": "Bar"}