From c50589dc59b0c2f9614166b53f9a3fcd18383fb0 Mon Sep 17 00:00:00 2001 From: Yurii Motov Date: Wed, 10 Dec 2025 17:22:24 +0100 Subject: [PATCH] Fix schema for nested model with computed field in response model --- fastapi/_compat/v2.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/fastapi/_compat/v2.py b/fastapi/_compat/v2.py index acd23d846..75bc673d2 100644 --- a/fastapi/_compat/v2.py +++ b/fastapi/_compat/v2.py @@ -171,11 +171,17 @@ def _get_model_config(model: BaseModel) -> Any: return model.model_config +def _model_has_computed_fields(model_or_enum: Any) -> bool: + if lenient_issubclass(model_or_enum, BaseModel): + model_schema = model_or_enum.__pydantic_core_schema__.get("schema", {}) + computed_fields = model_schema.get("computed_fields", []) + return len(computed_fields) > 0 + return False + + def _has_computed_fields(field: ModelField) -> bool: - computed_fields = field._type_adapter.core_schema.get("schema", {}).get( - "computed_fields", [] - ) - return len(computed_fields) > 0 + models = get_flat_models_from_field(field, known_models=set()) + return any(_model_has_computed_fields(model) for model in models) def get_schema_from_model_field(