Skip to content

Commit

Permalink
fix(extract_errors): fix wrong field being emitted when extracting se…
Browse files Browse the repository at this point in the history
…veral 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.
  • Loading branch information
fbessou authored and zupo committed Nov 8, 2024
1 parent af2bbdf commit b815bff
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pyramid_openapi3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion pyramid_openapi3/tests/test_extract_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b815bff

Please sign in to comment.