mirror of https://github.com/tiangolo/fastapi.git
🐛 Fix body parsing (#918)
This commit is contained in:
parent
d8451f75a4
commit
c425509d57
|
|
@ -634,7 +634,11 @@ async def request_body_to_args(
|
||||||
) and isinstance(received_body, FormData):
|
) and isinstance(received_body, FormData):
|
||||||
value = received_body.getlist(field.alias)
|
value = received_body.getlist(field.alias)
|
||||||
else:
|
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 (
|
if (
|
||||||
value is None
|
value is None
|
||||||
or (isinstance(field_info, params.Form) and value == "")
|
or (isinstance(field_info, params.Form) and value == "")
|
||||||
|
|
@ -645,18 +649,7 @@ async def request_body_to_args(
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
if field.required:
|
if field.required:
|
||||||
if PYDANTIC_1:
|
errors.append(get_missing_field_error(field.alias))
|
||||||
errors.append(
|
|
||||||
ErrorWrapper(MissingError(), loc=("body", field.alias))
|
|
||||||
)
|
|
||||||
else: # pragma: nocover
|
|
||||||
errors.append(
|
|
||||||
ErrorWrapper( # type: ignore
|
|
||||||
MissingError(),
|
|
||||||
loc=("body", field.alias),
|
|
||||||
config=BaseConfig,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
values[field.name] = deepcopy(field.default)
|
values[field.name] = deepcopy(field.default)
|
||||||
continue
|
continue
|
||||||
|
|
@ -685,6 +678,16 @@ async def request_body_to_args(
|
||||||
return values, errors
|
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:
|
def get_schema_compatible_field(*, field: ModelField) -> ModelField:
|
||||||
out_field = field
|
out_field = field
|
||||||
if lenient_issubclass(field.type_, UploadFile):
|
if lenient_issubclass(field.type_, UploadFile):
|
||||||
|
|
|
||||||
|
|
@ -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):
|
def test_post_body(path, body, expected_status, expected_response):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue