[QUESTION] How to change response model for "422 Unprocessable Entity" in OpenAPI docs
See original GitHub issueDescription
I am trying to return my own response model for 422 Unprocessable Entity
error.
I added a custom exception handler to my app, and add my response model type to responses
parameter of router.post()
.
def MyValidationError(BaseModel):
message: str
@app.exception_handler(RequestValidationError)
def handle_validation_error(request, exc) -> JSONResponse:
return JSONResponse(
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
content=MyValidationError('invalid parameter').dict(),
)
@router.post("/user", responses={422: {"model": MyValidationError}})
def foo(user_id: str, user_name: str):
...
/user
works as I expected.
But /openapi.json
still refers the default HTTPValidationError model.
{
...
"paths": {
"/user": {
"post": {
"responses": {
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
I found the HTTPValidationError
schema sesms to be hardcoded in fastapi/openapi/utils.py.
I expected that response models in openapi.json should reflect either custom exception handler for RequestValidationError
or responses
parameter for routes.
Is this a bug?
If not, is there any way to set correct response models for status 422
in documents?
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
FastApi 422 Unprocessable Entity, on authentication, how ...
Thus, changing the content type to www-form-urlencoded and adding a FormData object to your request's body, will make it work.
Read more >422 Unprocessable Entity - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates that the server understands the content type ...
Read more >Solved: Re: Receive error Code 422 Unprocessable Entity us...
Hi,. I am trying to call Canvas api to update on gradebook custom column from javascript. There's no problem when I use GET...
Read more >REST API - Flask AppBuilder - Read the Docs
We can define an OpenAPI specification by using YAML on the docs section of our methods: ... Next let's change our newly created...
Read more >API Docs - GitLab Docs
You can also use a partial OpenAPI definition, to test the API directly from the ... 422 Unprocessable, The entity couldn't be processed....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@rakesh4real https://fastapi.tiangolo.com/advanced/extending-openapi/
OK. I got it. I’ll try to modify the OpenAPI schema. Thanks!