From e6bc6ecf307c4017769bed1985a6bd733e2fa81c Mon Sep 17 00:00:00 2001 From: "boyi.zhang" Date: Sat, 21 Dec 2024 16:43:45 +0800 Subject: [PATCH 1/2] fix: bugfix/openapi/header_params_model --- fastapi/dependencies/utils.py | 15 +++++++++++---- fastapi/openapi/utils.py | 5 ++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index e2866b488..5c339c512 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -713,14 +713,21 @@ def _validate_value_with_model_field( return v_, [] -def _get_multidict_value( - field: ModelField, values: Mapping[str, Any], alias: Union[str, None] = None -) -> Any: - alias = alias or field.alias +def _get_value(field: ModelField, values: Mapping[str, Any], alias: Union[str, None] = None) -> Any: if is_sequence_field(field) and isinstance(values, (ImmutableMultiDict, Headers)): value = values.getlist(alias) else: value = values.get(alias, None) + return value + + +def _get_multidict_value( + field: ModelField, values: Mapping[str, Any], alias: Union[str, None] = None +) -> Any: + alias = alias or field.alias + value = _get_value(field, values, alias) + if value is None and alias is not None: + value = _get_value(field, values, field.alias) if ( value is None or ( diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 947eca948..c22aecfeb 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -126,8 +126,11 @@ def _get_openapi_operation_parameters( field_mapping=field_mapping, separate_input_output_schemas=separate_input_output_schemas, ) + param_name = param.alias + if param_type == ParamTypes.header and param_name: + param_name = param.alias.replace("_", "-") parameter = { - "name": param.alias, + "name": param_name, "in": param_type.value, "required": param.required, "schema": param_schema, From 75a84bd10597b90e05616a432354174cdc857f31 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 21 Dec 2024 12:27:46 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/dependencies/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 5c339c512..375d487c7 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -713,7 +713,9 @@ def _validate_value_with_model_field( return v_, [] -def _get_value(field: ModelField, values: Mapping[str, Any], alias: Union[str, None] = None) -> Any: +def _get_value( + field: ModelField, values: Mapping[str, Any], alias: Union[str, None] = None +) -> Any: if is_sequence_field(field) and isinstance(values, (ImmutableMultiDict, Headers)): value = values.getlist(alias) else: @@ -722,7 +724,7 @@ def _get_value(field: ModelField, values: Mapping[str, Any], alias: Union[str, N def _get_multidict_value( - field: ModelField, values: Mapping[str, Any], alias: Union[str, None] = None + field: ModelField, values: Mapping[str, Any], alias: Union[str, None] = None ) -> Any: alias = alias or field.alias value = _get_value(field, values, alias)