How to disable the default 422 doc

See original GitHub issue

FastAPI has a default response doc for 422, 1 but my response format is :

{
  'code': 0,
  'msg': 'hello',
  'data': <object>
}

and code contains any error code and msg contains any error info. I don’t need the default 422 response doc.

I found the code in fastapi/openapi/utils

http422 = str(HTTP_422_UNPROCESSABLE_ENTITY)
            if (all_route_params or route.body_field) and not any(
                [
                    status in operation["responses"]
                    for status in [http422, "4XX", "default"]
                ]
            ):
                operation["responses"][http422] = {
                    "description": "Validation Error",
                    "content": {
                        "application/json": {
                            "schema": {"$ref": REF_PREFIX + "HTTPValidationError"}
                        }
                    },
                }
                if "ValidationError" not in definitions:
                    definitions.update(
                        {
                            "ValidationError": validation_error_definition,
                            "HTTPValidationError": validation_error_response_definition,
                        }
                    )

I think many application will need this feature. Can you set an optional switch or else that I can disable the default 422 doc? @tiangolo thanks.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Jedorecommented, Oct 19, 2022

My newest solution:

app = FastAPI()

def custom_openapi():
    if not app.openapi_schema:
        app.openapi_schema = get_openapi(
            title=app.title,
            version=app.version,
            openapi_version=app.openapi_version,
            description=app.description,
            terms_of_service=app.terms_of_service,
            contact=app.contact,
            license_info=app.license_info,
            routes=app.routes,
            tags=app.openapi_tags,
            servers=app.servers,
        )
        for _, method_item in app.openapi_schema.get('paths').items():
            for _, param in method_item.items():
                responses = param.get('responses')
                # remove 422 response, also can remove other status code
                if '422' in responses:
                    del responses['422']
    return app.openapi_schema

app.openapi = custom_openapi

Hope this is useful. @vanntile @kounelios13

2reactions
vanntilecommented, Jul 27, 2022

@kounelios13 I think you misunderstood what @DionVitor meant. I have probably a similar problem. I need to return the validation error with a different status code, but 422 is still in the OpenAPI if I override the ValidationError in FastAPI exception_handlers. Any tips how to remove the entry for 422 in the OpenAPI?

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
Why does Swagger show "422 Validation Error" under the ...
When I visit the auto-generated swagger docs (http://127.0.0.1:8000/docs), my delete endpoint shows 422 Validation Error under Responses.
Read more >
Disable and replace TLS 1.0 in ADFS - Windows Server
Events ID 422 is logged on AD FS proxies: Unable to retrieve proxy configuration from the Federation Service. Proxies cannot forward traffic to ......
Read more >
Handling Errors - FastAPI
And it also includes a default exception handler for it. ... HTTP_422_UNPROCESSABLE_ENTITY, content=jsonable_encoder({"detail": exc.errors(), ...
Read more >
Instructions for Birth Certificate Order Form
To request this document in another format, call 1-800-525-0127. ... DOH 422-182 July 2021 ... **If you are not one of the listed...
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