Skip to content

Commit

Permalink
fix: Flask crashes after Pydantic V2 validator raises ValueError + co…
Browse files Browse the repository at this point in the history
…mpatibility issue in default_after_handler (#397)

* default_after_handler - support resp_validation_error in v2

* flask - support validator errors in request and response validation
  • Loading branch information
dror-fs authored Dec 31, 2024
1 parent f60b7e5 commit bca3067
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions spectree/plugins/flask_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ def validate(
self.request_validation(request, query, json, form, headers, cookies)
except (InternalValidationError, ValidationError) as err:
req_validation_error = err
response = make_response(jsonify(err.errors()), validation_error_status)
errors = err.errors() if isinstance(err, InternalValidationError) else err.errors(include_context=False)
response = make_response(jsonify(errors), validation_error_status)

if self.config.annotations:
annotations = get_type_hints(func)
Expand Down Expand Up @@ -224,7 +225,8 @@ def validate(
response_payload=payload,
)
except (InternalValidationError, ValidationError) as err:
response = make_response(err.errors(), 500)
errors = err.errors() if isinstance(err, InternalValidationError) else err.errors(include_context=False)
response = make_response(errors, 500)
resp_validation_error = err
else:
response = make_response(
Expand Down
3 changes: 2 additions & 1 deletion spectree/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ def default_after_handler(
if resp_validation_error:
logger.error(
"500 Response Validation Error: %s - %s",
resp_validation_error.model.__name__,
getattr(resp_validation_error, "title", None)
or resp_validation_error.model.__name__,
resp_validation_error.errors(),
)

Expand Down

0 comments on commit bca3067

Please sign in to comment.