From 01289a46fce7720df0b3aa0b75c6c0f64dab336f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Fri, 26 Dec 2025 20:28:28 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Restore=20and=20udpate=20test=20for?= =?UTF-8?q?=20coverage=20in=20jsonable=5Fencoder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_jsonable_encoder.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/test_jsonable_encoder.py b/tests/test_jsonable_encoder.py index c61dede8e..cdd3a47b5 100644 --- a/tests/test_jsonable_encoder.py +++ b/tests/test_jsonable_encoder.py @@ -5,7 +5,7 @@ from decimal import Decimal from enum import Enum from math import isinf, isnan from pathlib import PurePath, PurePosixPath, PureWindowsPath -from typing import Optional +from typing import Optional, TypedDict import pytest from fastapi._compat import Undefined @@ -202,6 +202,24 @@ def test_encode_model_with_default(): } +def test_custom_encoders(): + class safe_datetime(datetime): + pass + + class MyDict(TypedDict): + dt_field: safe_datetime + + instance = MyDict(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()