티스토리 뷰

공부

[FastApi] remove http_status 422

승가비 2023. 9. 16. 22:19
728x90
@app.get("/items/")
async def read_items():
    return [{"name": "Foo"}]


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    """Your custom validation exception."""
    message = 
    return JSONResponse(
        status_code=400,
        content={"message": f"Validation error: {exc}"}
    )


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi()

    # look for the error 422 and removes it
    for method in openapi_schema["paths"]:
        try:
            del openapi_schema["paths"][method]["post"]["responses"]["422"]
        except KeyError:
            pass

    app.openapi_schema = openapi_schema
    return app.openapi_schema


app.openapi = custom_openapi

https://stackoverflow.com/questions/61101878/fastapi-custom-error-handling-broke-automatic-documentation

 

FastAPI Custom Error Handling Broke Automatic Documentation

For our requirements, I made the following changes I updated the input validation exception code from 422 to 400. I also modified the default Json error output. My issue My FastAPI generated aut...

stackoverflow.com

 

728x90
댓글