From cc96fd3a7cf74972b57a6439f22d858a2ae42c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 27 Jul 2026 18:17:33 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Avoid=20flattening=20depen?= =?UTF-8?q?dencies=20for=20request=20parameters,=20mainly=20for=20OpenAPI?= =?UTF-8?q?=20(#16073)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/dependencies/utils.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 2799269397..dedb394327 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -242,11 +242,31 @@ def _get_flat_fields_from_params(fields: list[ModelField]) -> list[ModelField]: def get_flat_params(dependant: Dependant) -> list[ModelField]: - flat_dependant = get_flat_dependant(dependant, skip_repeats=True) - path_params = _get_flat_fields_from_params(flat_dependant.path_params) - query_params = _get_flat_fields_from_params(flat_dependant.query_params) - header_params = _get_flat_fields_from_params(flat_dependant.header_params) - cookie_params = _get_flat_fields_from_params(flat_dependant.cookie_params) + path_params: list[ModelField] = [] + query_params: list[ModelField] = [] + header_params: list[ModelField] = [] + cookie_params: list[ModelField] = [] + visited: list[DependencyCacheKey] = [] + uses_scopes_cache: _UsesScopesCache = {} + dependants = [dependant] + while dependants: + current_dependant = dependants.pop() + cache_key = _get_cache_key( + dependant=current_dependant, + uses_scopes_cache=uses_scopes_cache, + ) + if cache_key in visited: + continue + visited.append(cache_key) + path_params.extend(current_dependant.path_params) + query_params.extend(current_dependant.query_params) + header_params.extend(current_dependant.header_params) + cookie_params.extend(current_dependant.cookie_params) + dependants.extend(reversed(current_dependant.dependencies)) + path_params = _get_flat_fields_from_params(path_params) + query_params = _get_flat_fields_from_params(query_params) + header_params = _get_flat_fields_from_params(header_params) + cookie_params = _get_flat_fields_from_params(cookie_params) return path_params + query_params + header_params + cookie_params