Browse Source

fix handling form data with fields that are not annotated as Form()

pull/13464/head
sneakers-the-rat 4 weeks ago
parent
commit
64f25284e8
No known key found for this signature in database GPG Key ID: 6DCB96EF1E4D232D
  1. 12
      fastapi/dependencies/utils.py

12
fastapi/dependencies/utils.py

@ -717,7 +717,7 @@ def _get_multidict_value(
field: ModelField, field: ModelField,
values: Mapping[str, Any], values: Mapping[str, Any],
alias: Union[str, None] = None, alias: Union[str, None] = None,
return_default: bool = True, form_input: bool = False,
) -> Any: ) -> Any:
alias = alias or field.alias alias = alias or field.alias
if is_sequence_field(field) and isinstance(values, (ImmutableMultiDict, Headers)): if is_sequence_field(field) and isinstance(values, (ImmutableMultiDict, Headers)):
@ -727,16 +727,16 @@ def _get_multidict_value(
if ( if (
value is None value is None
or ( or (
isinstance(field.field_info, params.Form) (isinstance(field.field_info, params.Form) or form_input)
and isinstance(value, str) # For type checks and isinstance(value, str) # For type checks
and value == "" and value == ""
) )
or (is_sequence_field(field) and len(value) == 0) or (is_sequence_field(field) and len(value) == 0)
): ):
if return_default and not field.required: if form_input or field.required:
return deepcopy(field.default)
else:
return None return None
else:
return deepcopy(field.default)
return value return value
@ -843,7 +843,7 @@ async def _extract_form_body(
first_field_info = first_field.field_info first_field_info = first_field.field_info
for field in body_fields: for field in body_fields:
value = _get_multidict_value(field, received_body, return_default=False) value = _get_multidict_value(field, received_body, form_input=True)
if ( if (
isinstance(first_field_info, params.File) isinstance(first_field_info, params.File)
and is_bytes_field(field) and is_bytes_field(field)

Loading…
Cancel
Save