Browse Source

🐛 Fix body parsing (#918)

pull/948/head
David Montague 5 years ago
committed by GitHub
parent
commit
c425509d57
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      fastapi/dependencies/utils.py
  2. 24
      tests/test_tutorial/test_body_multiple_params/test_tutorial003.py

29
fastapi/dependencies/utils.py

@ -634,7 +634,11 @@ async def request_body_to_args(
) and isinstance(received_body, FormData):
value = received_body.getlist(field.alias)
else:
value = received_body.get(field.alias)
try:
value = received_body.get(field.alias)
except AttributeError:
errors.append(get_missing_field_error(field.alias))
continue
if (
value is None
or (isinstance(field_info, params.Form) and value == "")
@ -645,18 +649,7 @@ async def request_body_to_args(
)
):
if field.required:
if PYDANTIC_1:
errors.append(
ErrorWrapper(MissingError(), loc=("body", field.alias))
)
else: # pragma: nocover
errors.append(
ErrorWrapper( # type: ignore
MissingError(),
loc=("body", field.alias),
config=BaseConfig,
)
)
errors.append(get_missing_field_error(field.alias))
else:
values[field.name] = deepcopy(field.default)
continue
@ -685,6 +678,16 @@ async def request_body_to_args(
return values, errors
def get_missing_field_error(field_alias: str) -> ErrorWrapper:
if PYDANTIC_1:
missing_field_error = ErrorWrapper(MissingError(), loc=("body", field_alias))
else: # pragma: no cover
missing_field_error = ErrorWrapper( # type: ignore
MissingError(), loc=("body", field_alias), config=BaseConfig,
)
return missing_field_error
def get_schema_compatible_field(*, field: ModelField) -> ModelField:
out_field = field
if lenient_issubclass(field.type_, UploadFile):

24
tests/test_tutorial/test_body_multiple_params/test_tutorial003.py

@ -166,6 +166,30 @@ def test_openapi_schema():
]
},
),
(
"/items/5",
[],
422,
{
"detail": [
{
"loc": ["body", "item"],
"msg": "field required",
"type": "value_error.missing",
},
{
"loc": ["body", "user"],
"msg": "field required",
"type": "value_error.missing",
},
{
"loc": ["body", "importance"],
"msg": "field required",
"type": "value_error.missing",
},
]
},
),
],
)
def test_post_body(path, body, expected_status, expected_response):

Loading…
Cancel
Save