Skip to content

Commit 4c44017

Browse files
[FIX] fastapi: support both starlette < and >= 0.48
Starting from `starlette >= 0.48`, the `status.HTTP_422_UNPROCESSABLE_ENTITY` emits a deprecation warning, as it has been replaced by `status.HTTP_422_UNPROCESSABLE_CONTENT`. To be compatible with past and future versions, we simply use the integer value `422`, aligning also with the updated `fastapi` documentation: See: - fastapi/fastapi#14077
1 parent e8d767d commit 4c44017

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

fastapi/error_handlers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ def convert_exception_to_status_body(exc: Exception) -> tuple[int, dict]:
2929
status_code = exc.status_code
3030
details = exc.detail
3131
elif isinstance(exc, RequestValidationError):
32-
status_code = status.HTTP_422_UNPROCESSABLE_CONTENT
32+
# Use integer status code to supress starlette >= 0.48 deprecation warning
33+
# See: https://github.com/fastapi/fastapi/pull/14077
34+
# status_code = status.HTTP_422_UNPROCESSABLE_CONTENT
35+
status_code = 422
3336
details = jsonable_encoder(exc.errors())
3437
elif isinstance(exc, WebSocketRequestValidationError):
3538
status_code = status.WS_1008_POLICY_VIOLATION

fastapi/tests/test_fastapi.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ def test_request_validation_error(self) -> None:
156156
route = "/fastapi_demo/demo/exception?exception_type=BAD&error_message="
157157
response = self.url_open(route, timeout=200)
158158
mocked_commit.assert_not_called()
159-
self.assertEqual(
160-
response.status_code, status.HTTP_422_UNPROCESSABLE_CONTENT
161-
)
159+
self.assertEqual(response.status_code, 422)
162160

163161
def test_no_commit_on_exception(self) -> None:
164162
# this test check that the way we mock the cursor is working as expected

0 commit comments

Comments
 (0)