From b815bff5e562deff2fcb56931f7b920534347261 Mon Sep 17 00:00:00 2001 From: Frank Bessou Date: Fri, 8 Nov 2024 15:31:25 +0100 Subject: [PATCH] fix(extract_errors): fix wrong field being emitted when extracting several errors for a same object The `field` variable was used both as the function argument and as a local variable that was not intended to be reused between iterations. Renaming the function argument fixes this problem since its value is no longer modified between iterations. --- pyramid_openapi3/exceptions.py | 4 ++-- pyramid_openapi3/tests/test_extract_errors.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyramid_openapi3/exceptions.py b/pyramid_openapi3/exceptions.py index 23786e7..f1372d4 100644 --- a/pyramid_openapi3/exceptions.py +++ b/pyramid_openapi3/exceptions.py @@ -69,7 +69,7 @@ class ImproperAPISpecificationWarning(UserWarning): def extract_errors( - request: Request, errors: t.List[OpenAPIError], field: t.Optional[str] = None + request: Request, errors: t.List[OpenAPIError], parent_field: t.Optional[str] = None ) -> t.Iterator[t.Dict[str, str]]: """Extract errors for JSON response. @@ -129,7 +129,7 @@ def extract_errors( output.update({"message": message}) - field = getattr(err, "field", field) + field = getattr(err, "field", parent_field) if field is None: field = getattr(err, "name", None) if field is None and getattr(err, "validator", None) == "required": diff --git a/pyramid_openapi3/tests/test_extract_errors.py b/pyramid_openapi3/tests/test_extract_errors.py index ad4d502..65c6f50 100644 --- a/pyramid_openapi3/tests/test_extract_errors.py +++ b/pyramid_openapi3/tests/test_extract_errors.py @@ -397,7 +397,7 @@ def test_multiple_errors(self) -> None: { "exception": "ParameterValidationError", "message": "Failed to cast value to integer type: abc", - "field": "bar", + "field": "bam", }, { "exception": "ValidationError",