Skip to content

Overriding Validation Error default schema used in API document #660

@otsuka

Description

@otsuka

In my app, I customize RequestValidationError response using exception_handler as below:

@app.exception_handler(RequestValidationError)
async def request_validation_exception_handler(_request: Request, exc: RequestValidationError) -> JSONResponse:
    data = {
        "error": {
            "code": Error.REQUEST_VALIDATION_ERROR.value,
            "message": "Intput data is invalid.",
            "detail": exc.errors(),
        }
    }
    return JSONResponse(content=data, status_code=HTTP_422_UNPROCESSABLE_ENTITY)

The generated API document includes automatically the default Validation Error schema, HTTPValidationError, in all POST or PUT path operations.
So I need to add my responses definition one by one.

responses = {
    422: {'model': APIErrorResponse, 'description': 'Validation Error'}
}


@router.post("/", responses={**responses})
async def create_user(user_in: UserCreate):
    """Create new user.
    """

@router.put("/", responses={**responses})
async def update_user(user_in: UserUpdate):
    """Update user.
    """

It would be useful if we are able to override the Validation Error default schema used in API document.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions