Browse Source

Tweak utils and tests

pull/13657/head
Kinuax 3 months ago
parent
commit
9b5b74bc45
  1. 20
      fastapi/dependencies/utils.py
  2. 5
      fastapi/utils.py
  3. 10
      tests/test_validation_error_fields.py

20
fastapi/dependencies/utils.py

@ -709,13 +709,12 @@ def _validate_value_with_model_field(
if value is None: if value is None:
if field.required: if field.required:
if PYDANTIC_V2: if PYDANTIC_V2:
return None, [ error = get_missing_field_error(
get_missing_field_error( loc, field.include_error_input, field.include_error_url
loc, field.include_error_input, field.include_error_url )
)
]
else: else:
return None, [get_missing_field_error(loc=loc)] error = get_missing_field_error(loc)
return None, [error]
else: else:
return deepcopy(field.default), [] return deepcopy(field.default), []
v_, errors_ = field.validate(value, values, loc=loc) v_, errors_ = field.validate(value, values, loc=loc)
@ -931,13 +930,12 @@ async def request_body_to_args(
# If the received body is a list, not a dict # If the received body is a list, not a dict
except AttributeError: except AttributeError:
if PYDANTIC_V2: if PYDANTIC_V2:
errors.append( error = get_missing_field_error(
get_missing_field_error( loc, field.include_error_input, field.include_error_url
loc, field.include_error_input, field.include_error_url
)
) )
else: else:
errors.append(get_missing_field_error(loc)) error = get_missing_field_error(loc)
errors.append(error)
continue continue
v_, errors_ = _validate_value_with_model_field( v_, errors_ = _validate_value_with_model_field(
field=field, value=value, values=values, loc=loc field=field, value=value, values=values, loc=loc

5
fastapi/utils.py

@ -80,10 +80,7 @@ def create_model_field(
) )
else: else:
field_info = field_info or FieldInfo() field_info = field_info or FieldInfo()
kwargs = { kwargs = {"name": name, "field_info": field_info}
"name": name,
"field_info": field_info,
}
if PYDANTIC_V2: if PYDANTIC_V2:
kwargs.update( kwargs.update(
{ {

10
tests/test_validation_error_fields.py

@ -23,17 +23,19 @@ def test_input_and_url_fields(include_error_input, include_error_url):
def path2(query_param: int): def path2(query_param: int):
return query_param return query_param
router = APIRouter() router = APIRouter(
include_error_input=include_error_input, include_error_url=include_error_url
)
@app.get("/path3/{path_param}") @router.get("/path3/{path_param}")
def path3(path_param: int): def path3(path_param: int):
return {"path_param": path_param} return {"path_param": path_param}
@app.get("/path4/") @router.get("/path4/")
def path4(query_param: int): def path4(query_param: int):
return query_param return query_param
app.include_router(router, prefix="/prefix") app.include_router(router)
client = TestClient(app) client = TestClient(app)
with client: with client:

Loading…
Cancel
Save