Browse Source

Removing stale code and tests for encoder

pull/14333/head
Saurav Venkat 8 months ago
parent
commit
b7067f59a9
  1. 12
      fastapi/encoders.py
  2. 54
      tests/test_jsonable_encoder.py

12
fastapi/encoders.py

@ -210,13 +210,11 @@ def jsonable_encoder(
[FastAPI docs for JSON Compatible Encoder](https://fastapi.tiangolo.com/tutorial/encoder/).
"""
custom_encoder = custom_encoder or {}
if custom_encoder:
if type(obj) in custom_encoder:
return custom_encoder[type(obj)](obj)
else:
for encoder_type, encoder_instance in custom_encoder.items():
if isinstance(obj, encoder_type):
return encoder_instance(obj)
if type(obj) in custom_encoder:
return custom_encoder[type(obj)](obj)
for encoder_type, encoder_instance in custom_encoder.items():
if isinstance(obj, encoder_type):
return encoder_instance(obj)
if include is not None and not isinstance(include, (set, dict)):
include = set(include)
if exclude is not None and not isinstance(exclude, (set, dict)):

54
tests/test_jsonable_encoder.py

@ -11,7 +11,7 @@ from fastapi._compat import PYDANTIC_V2, Undefined
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel, Field, ValidationError
from .utils import needs_pydanticv1, needs_pydanticv2
from .utils import needs_pydanticv2
class Person:
@ -149,29 +149,6 @@ def test_encode_custom_json_encoders_model_pydanticv2():
assert jsonable_encoder(subclass_model) == {"dt_field": "2019-01-01T08:00:00+00:00"}
# TODO: remove when deprecating Pydantic v1
@needs_pydanticv1
def test_encode_custom_json_encoders_model_pydanticv1():
class ModelWithCustomEncoder(BaseModel):
dt_field: datetime
class Config:
json_encoders = {
datetime: lambda dt: dt.replace(
microsecond=0, tzinfo=timezone.utc
).isoformat()
}
class ModelWithCustomEncoderSubclass(ModelWithCustomEncoder):
class Config:
pass
model = ModelWithCustomEncoder(dt_field=datetime(2019, 1, 1, 8))
assert jsonable_encoder(model) == {"dt_field": "2019-01-01T08:00:00+00:00"}
subclass_model = ModelWithCustomEncoderSubclass(dt_field=datetime(2019, 1, 1, 8))
assert jsonable_encoder(subclass_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"}
@ -204,26 +181,6 @@ def test_encode_model_with_default():
"bla": "bla",
}
@needs_pydanticv1
def test_custom_encoders():
class safe_datetime(datetime):
pass
class MyModel(BaseModel):
dt_field: safe_datetime
instance = MyModel(dt_field=safe_datetime.now())
encoded_instance = jsonable_encoder(
instance, custom_encoder={safe_datetime: lambda o: o.strftime("%H:%M:%S")}
)
assert encoded_instance["dt_field"] == instance.dt_field.strftime("%H:%M:%S")
encoded_instance2 = jsonable_encoder(instance)
assert encoded_instance2["dt_field"] == instance.dt_field.isoformat()
def test_custom_enum_encoders():
def custom_enum_encoder(v: Enum):
return v.value.lower()
@ -285,15 +242,6 @@ def test_encode_model_with_pure_windows_path():
assert jsonable_encoder(obj) == {"path": "\\foo\\bar"}
@needs_pydanticv1
def test_encode_root():
class ModelWithRoot(BaseModel):
__root__: str
model = ModelWithRoot(__root__="Foo")
assert jsonable_encoder(model) == "Foo"
@needs_pydanticv2
def test_decimal_encoder_float():
data = {"value": Decimal(1.23)}

Loading…
Cancel
Save