From b7067f59a986618f91d37e53ea3cb516d5aeedc6 Mon Sep 17 00:00:00 2001 From: Saurav Venkat Date: Mon, 10 Nov 2025 15:40:19 -0500 Subject: [PATCH] Removing stale code and tests for encoder --- fastapi/encoders.py | 12 ++++---- tests/test_jsonable_encoder.py | 54 +--------------------------------- 2 files changed, 6 insertions(+), 60 deletions(-) diff --git a/fastapi/encoders.py b/fastapi/encoders.py index 6fc6228e1..90baeb347 100644 --- a/fastapi/encoders.py +++ b/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)): diff --git a/tests/test_jsonable_encoder.py b/tests/test_jsonable_encoder.py index 447c5b4d6..dc4d659b8 100644 --- a/tests/test_jsonable_encoder.py +++ b/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)}