From f75daed0a07dd013650dca7b5db6dfb34890f5d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Fri, 12 Dec 2025 15:08:49 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20extracting=20attributes=20?= =?UTF-8?q?for=20old=20Pydantic=20versions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/_compat/v2.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fastapi/_compat/v2.py b/fastapi/_compat/v2.py index d7150498f..eb5c06edd 100644 --- a/fastapi/_compat/v2.py +++ b/fastapi/_compat/v2.py @@ -78,10 +78,15 @@ _Attrs = { # TODO: remove when dropping support for Pydantic < v2.12.3 def asdict(field_info: FieldInfo) -> Dict[str, Any]: + attributes = {} + for attr in _Attrs: + value = getattr(field_info, attr, Undefined) + if value is not Undefined: + attributes[attr] = value return { "annotation": field_info.annotation, "metadata": field_info.metadata, - "attributes": {attr: getattr(field_info, attr) for attr in _Attrs}, + "attributes": attributes, }