mirror of https://github.com/tiangolo/fastapi.git
🐛 Fix jsonable_encoder for models with Config (#29)
but without json_encoders
This commit is contained in:
parent
92c825be6a
commit
9745a5d1ae
|
|
@ -15,17 +15,12 @@ def jsonable_encoder(
|
||||||
custom_encoder: dict = {},
|
custom_encoder: dict = {},
|
||||||
) -> Any:
|
) -> Any:
|
||||||
if isinstance(obj, BaseModel):
|
if isinstance(obj, BaseModel):
|
||||||
if not obj.Config.json_encoders:
|
encoder = getattr(obj.Config, "json_encoders", custom_encoder)
|
||||||
return jsonable_encoder(
|
return jsonable_encoder(
|
||||||
obj.dict(include=include, exclude=exclude, by_alias=by_alias),
|
obj.dict(include=include, exclude=exclude, by_alias=by_alias),
|
||||||
include_none=include_none,
|
include_none=include_none,
|
||||||
)
|
custom_encoder=encoder,
|
||||||
else:
|
)
|
||||||
return jsonable_encoder(
|
|
||||||
obj.dict(include=include, exclude=exclude, by_alias=by_alias),
|
|
||||||
include_none=include_none,
|
|
||||||
custom_encoder=obj.Config.json_encoders,
|
|
||||||
)
|
|
||||||
if isinstance(obj, Enum):
|
if isinstance(obj, Enum):
|
||||||
return obj.value
|
return obj.value
|
||||||
if isinstance(obj, (str, int, float, type(None))):
|
if isinstance(obj, (str, int, float, type(None))):
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import json
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
@ -18,7 +17,7 @@ class ModelWithDatetimeField(BaseModel):
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
model = ModelWithDatetimeField(dt_field=datetime.utcnow())
|
model = ModelWithDatetimeField(dt_field=datetime(2019, 1, 1, 8))
|
||||||
|
|
||||||
|
|
||||||
@app.get("/model", response_model=ModelWithDatetimeField)
|
@app.get("/model", response_model=ModelWithDatetimeField)
|
||||||
|
|
@ -32,4 +31,4 @@ client = TestClient(app)
|
||||||
def test_dt():
|
def test_dt():
|
||||||
with client:
|
with client:
|
||||||
response = client.get("/model")
|
response = client.get("/model")
|
||||||
assert json.loads(model.json()) == response.json()
|
assert response.json() == {"dt_field": "2019-01-01T08:00:00+00:00"}
|
||||||
|
|
@ -1,5 +1,9 @@
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.encoders import jsonable_encoder
|
from fastapi.encoders import jsonable_encoder
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
class Person:
|
class Person:
|
||||||
|
|
@ -32,6 +36,29 @@ class Unserializable:
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
|
class ModelWithCustomEncoder(BaseModel):
|
||||||
|
dt_field: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
json_encoders = {
|
||||||
|
datetime: lambda dt: dt.replace(
|
||||||
|
microsecond=0, tzinfo=timezone.utc
|
||||||
|
).isoformat()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class RoleEnum(Enum):
|
||||||
|
admin = "admin"
|
||||||
|
normal = "normal"
|
||||||
|
|
||||||
|
|
||||||
|
class ModelWithConfig(BaseModel):
|
||||||
|
role: RoleEnum = None
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
use_enum_values = True
|
||||||
|
|
||||||
|
|
||||||
def test_encode_class():
|
def test_encode_class():
|
||||||
person = Person(name="Foo")
|
person = Person(name="Foo")
|
||||||
pet = Pet(owner=person, name="Firulais")
|
pet = Pet(owner=person, name="Firulais")
|
||||||
|
|
@ -48,3 +75,13 @@ def test_encode_unsupported():
|
||||||
unserializable = Unserializable()
|
unserializable = Unserializable()
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
jsonable_encoder(unserializable)
|
jsonable_encoder(unserializable)
|
||||||
|
|
||||||
|
|
||||||
|
def test_encode_custom_json_encoders_model():
|
||||||
|
model = ModelWithCustomEncoder(dt_field=datetime(2019, 1, 1, 8))
|
||||||
|
assert jsonable_encoder(model) == {"dt_field": "2019-01-01T08:00:00+00:00"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_encode_model_with_config():
|
||||||
|
model = ModelWithConfig(role=RoleEnum.admin)
|
||||||
|
assert jsonable_encoder(model) == {"role": "admin"}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue