Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions openapi_core/validation/request/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class RequestValidationError(ValidationError):

class RequestBodyValidationError(RequestValidationError):
def __str__(self) -> str:
if self.__cause__ is not None:
return f"Request body validation error: {self.__cause__}"
return "Request body validation error"


Expand Down
25 changes: 25 additions & 0 deletions tests/integration/validation/test_strict_json_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,31 @@ def test_request_validator_urlencoded_json_part_strict() -> None:
validator.validate(request)


def test_request_validator_error_message_includes_cause_details() -> None:
spec = _spec_schema_path()
validator = V30RequestValidator(spec)

request_json = {
"id": "123e4567-e89b-12d3-a456-426614174000",
"username": "Test User",
"age": "30",
}
request = MockRequest(
"http://example.com",
"post",
"/users",
content_type="application/json",
data=json.dumps(request_json).encode("utf-8"),
)

with pytest.raises(InvalidRequestBody) as exc_info:
validator.validate(request)

error_message = str(exc_info.value)
assert error_message.startswith("Request body validation error:")
assert "'30' is not of type 'integer'" in error_message


def test_response_validator_strict_json_nested_types() -> None:
"""Test that nested JSON structures (arrays, objects) remain strict."""
spec_dict = {
Expand Down
Loading