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.

Custom RequestValidationError but Swagger not update the schema

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    errors = exc.errors()
    custom_error = []
    for error in errors:
        custom_error.append(error['msg'])
    return JSONResponse(status_code=422, content=jsonable_encoder({'detail': custom_error}))

Description

After I custom the RequestValidationError above, the swagger’s schema is still the original one.

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.79.0

Python Version

3.9.13

Additional Context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JarroVGITcommented, Aug 12, 2022

It’s described here in the docs . You can add a 422 response in the responses parameter of the decorator and that will override the default.

1reaction
JarroVGITcommented, Aug 12, 2022

@iudeen is correct, adding a handler does not automagically add that to the generated openapi.json. You would have to add those to the endpoints themselves in order for them to show up in the spec, otherwise it will use the default definition:

validation_error_definition = {
    "title": "ValidationError",
    "type": "object",
    "properties": {
        "loc": {
            "title": "Location",
            "type": "array",
            "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
        },
        "msg": {"title": "Message", "type": "string"},
        "type": {"title": "Error Type", "type": "string"},
    },
    "required": ["loc", "msg", "type"],
}

validation_error_response_definition = {
    "title": "HTTPValidationError",
    "type": "object",
    "properties": {
        "detail": {
            "title": "Detail",
            "type": "array",
            "items": {"$ref": REF_PREFIX + "ValidationError"},
        }
    },
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom exception handling not updating OpenAPI status code
Hi, I'm trying to override the 422 status code to a 400 across every endpoint on my app, but it keeps showing as...
Read more >
Displaying of FastAPI validation errors to end users
Use it to display form validation messages; Generate forms themselves from api description if it's possible. python · swagger · openapi ...
Read more >
Handling Errors - FastAPI
Override request validation exceptions¶. When a request contains invalid data, FastAPI internally raises a RequestValidationError . And it also includes a ...
Read more >
Fix Swagger Validator errors in Power Platform connectors
The Swagger Validator tool validates the connector files you submit in the GitHub open-source repository and the ISV portal.
Read more >
Using OpenAPI and Swagger UI - Quarkus
If you do not like the default endpoint location /q/openapi , you can change it by adding the following configuration to your application.properties...
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