Browse Source

chore: use HTTP_422_UNPROCESSABLE_CONTENT instead of literal 422

Use HTTP_422_UNPROCESSABLE_CONTENT wherever literal 422 was used in
fastapi/fastapi#14077.
pull/15196/head
Vishnu D Sasidharan 4 months ago
parent
commit
edd4ae4db3
  1. 4
      docs_src/handling_errors/tutorial005_py310.py
  2. 4
      fastapi/exception_handlers.py
  3. 3
      fastapi/openapi/utils.py

4
docs_src/handling_errors/tutorial005_py310.py

@ -1,4 +1,4 @@
from fastapi import FastAPI, Request
from fastapi import FastAPI, Request, status
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
@ -10,7 +10,7 @@ app = FastAPI()
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
status_code=422,
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}),
)

4
fastapi/exception_handlers.py

@ -5,7 +5,7 @@ from fastapi.websockets import WebSocket
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from starlette.status import WS_1008_POLICY_VIOLATION
from starlette.status import HTTP_422_UNPROCESSABLE_CONTENT, WS_1008_POLICY_VIOLATION
async def http_exception_handler(request: Request, exc: HTTPException) -> Response:
@ -21,7 +21,7 @@ async def request_validation_exception_handler(
request: Request, exc: RequestValidationError
) -> JSONResponse:
return JSONResponse(
status_code=422,
status_code=HTTP_422_UNPROCESSABLE_CONTENT,
content={"detail": jsonable_encoder(exc.errors())},
)

3
fastapi/openapi/utils.py

@ -38,6 +38,7 @@ from fastapi.utils import (
from pydantic import BaseModel
from starlette.responses import JSONResponse
from starlette.routing import BaseRoute
from starlette.status import HTTP_422_UNPROCESSABLE_CONTENT
validation_error_definition = {
"title": "ValidationError",
@ -451,7 +452,7 @@ def get_openapi_path(
)
deep_dict_update(openapi_response, process_response)
openapi_response["description"] = description
http422 = "422"
http422 = str(HTTP_422_UNPROCESSABLE_CONTENT)
all_route_params = get_flat_params(route.dependant)
if (all_route_params or route.body_field) and not any(
status in operation["responses"]

Loading…
Cancel
Save