Skip to content

Commit

Permalink
🐛 Fix body parsing (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Feb 4, 2020
1 parent d8451f7 commit c425509
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
29 changes: 16 additions & 13 deletions fastapi/dependencies/utils.py
Expand Up @@ -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 == "")
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
24 changes: 24 additions & 0 deletions tests/test_tutorial/test_body_multiple_params/test_tutorial003.py
Expand Up @@ -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):
Expand Down

0 comments on commit c425509

Please sign in to comment.