Add test for JSON decode error handling in long documents with early errors.

This commit is contained in:
Arif Dogan 2025-09-19 16:42:42 +02:00
parent ac50938ea1
commit 7a8da3a4ad
No known key found for this signature in database
GPG Key ID: A2131177E83422CE
1 changed files with 18 additions and 0 deletions

View File

@ -121,3 +121,21 @@ def test_json_decode_error_unclosed_brace():
assert "column" in error["msg"].lower()
assert error["type"] == "json_invalid"
assert "position" in error["ctx"]
def test_json_decode_error_in_middle_of_long_document():
# Create a JSON with error early in a long document (need >40 chars after error)
# The error is at position for "invalid" which needs at least 41 chars after it
long_json = '{"field": invalid, "padding_field": "this needs to be long enough that we have more than forty characters after the error position"}'
response = client.post(
"/items/", content=long_json, headers={"Content-Type": "application/json"}
)
assert response.status_code == 422
error = response.json()["detail"][0]
# The error snippet should have "..." at the end since error is early in doc
assert error["input"].endswith("...")
assert "invalid" in error["input"]
assert error["type"] == "json_invalid"