question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How to change response model for "422 Unprocessable Entity" in OpenAPI docs

See original GitHub issue

Description 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:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kokumuracommented, Jun 24, 2019

OK. I got it. I’ll try to modify the OpenAPI schema. Thanks!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found