mirror of https://github.com/tiangolo/fastapi.git
align tests with intended functionality
This commit is contained in:
parent
147ca63107
commit
687dd65c31
|
|
@ -344,9 +344,16 @@ def remove_invalid(v: Any, handler: Callable[[Any], Any]) -> Any:
|
|||
v.pop(loc[0], None)
|
||||
elif len(loc) == 2 and isinstance(v.get(loc[0]), list):
|
||||
try:
|
||||
v[loc[0]].pop(int(loc[1]))
|
||||
v[loc[0]][loc[1]] = None
|
||||
except (ValueError, IndexError):
|
||||
pass
|
||||
# remove the None values from lists
|
||||
for key in list(v.keys()):
|
||||
if isinstance(v[key], list):
|
||||
v[key] = [item for item in v[key] if item is not None]
|
||||
# remove empty lists
|
||||
if v[key] == []:
|
||||
v.pop(key)
|
||||
return handler(v)
|
||||
|
||||
|
||||
|
|
@ -519,13 +526,6 @@ def analyze_param(
|
|||
|
||||
field_info.alias = alias
|
||||
|
||||
if is_scalar_sequence_field(field) or is_scalar_sequence_mapping_field(field):
|
||||
# Wrap the validator to remove invalid values from scalar sequence
|
||||
# and scalar sequence mapping fields instead of failing the whole validation
|
||||
field_info.metadata = getattr(field_info, "metadata", []) + [
|
||||
WrapValidator(remove_invalid)
|
||||
]
|
||||
|
||||
field = create_model_field(
|
||||
name=param_name,
|
||||
type_=use_annotation_from_field_info,
|
||||
|
|
@ -550,6 +550,21 @@ def analyze_param(
|
|||
and getattr(field, "shape", 1) == 1
|
||||
)
|
||||
)
|
||||
if is_scalar_sequence_field(field) or is_scalar_sequence_mapping_field(
|
||||
field
|
||||
):
|
||||
field_info.metadata = getattr(field_info, "metadata", []) + [
|
||||
WrapValidator(remove_invalid)
|
||||
]
|
||||
field = create_model_field(
|
||||
name=param_name,
|
||||
type_=use_annotation_from_field_info,
|
||||
default=field_info.default,
|
||||
alias=alias,
|
||||
required=field_info.default
|
||||
in (RequiredParam, may_v1.RequiredParam, Undefined),
|
||||
field_info=field_info,
|
||||
)
|
||||
|
||||
return ParamDetails(type_annotation=type_annotation, depends=depends, field=field)
|
||||
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ def test_foo_needy_very(client: TestClient):
|
|||
assert response.json() == {
|
||||
"query": 2,
|
||||
"mapping_query_str_or_int": {"foo": "baz"},
|
||||
"mapping_query_int": None,
|
||||
"sequence_mapping_int": None,
|
||||
"mapping_query_int": {},
|
||||
"sequence_mapping_int": {},
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue