From 9b5b74bc45a4e26997d1b4f3dbe37446be695962 Mon Sep 17 00:00:00 2001 From: Kinuax Date: Fri, 2 May 2025 19:25:33 +0200 Subject: [PATCH] Tweak utils and tests --- fastapi/dependencies/utils.py | 20 +++++++++----------- fastapi/utils.py | 5 +---- tests/test_validation_error_fields.py | 10 ++++++---- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index a53e65c16..52d1a48a4 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -709,13 +709,12 @@ def _validate_value_with_model_field( if value is None: if field.required: if PYDANTIC_V2: - return None, [ - get_missing_field_error( - loc, field.include_error_input, field.include_error_url - ) - ] + error = get_missing_field_error( + loc, field.include_error_input, field.include_error_url + ) else: - return None, [get_missing_field_error(loc=loc)] + error = get_missing_field_error(loc) + return None, [error] else: return deepcopy(field.default), [] 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 except AttributeError: if PYDANTIC_V2: - errors.append( - get_missing_field_error( - loc, field.include_error_input, field.include_error_url - ) + error = get_missing_field_error( + loc, field.include_error_input, field.include_error_url ) else: - errors.append(get_missing_field_error(loc)) + error = get_missing_field_error(loc) + errors.append(error) continue v_, errors_ = _validate_value_with_model_field( field=field, value=value, values=values, loc=loc diff --git a/fastapi/utils.py b/fastapi/utils.py index 110e17e1f..b4cd55369 100644 --- a/fastapi/utils.py +++ b/fastapi/utils.py @@ -80,10 +80,7 @@ def create_model_field( ) else: field_info = field_info or FieldInfo() - kwargs = { - "name": name, - "field_info": field_info, - } + kwargs = {"name": name, "field_info": field_info} if PYDANTIC_V2: kwargs.update( { diff --git a/tests/test_validation_error_fields.py b/tests/test_validation_error_fields.py index 72e65f733..efe117ebb 100644 --- a/tests/test_validation_error_fields.py +++ b/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): 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): return {"path_param": path_param} - @app.get("/path4/") + @router.get("/path4/") def path4(query_param: int): return query_param - app.include_router(router, prefix="/prefix") + app.include_router(router) client = TestClient(app) with client: