Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to customize API validation error response in Flask #564

Open
rohan-97 opened this issue Apr 27, 2023 · 2 comments
Open

How to customize API validation error response in Flask #564

rohan-97 opened this issue Apr 27, 2023 · 2 comments
Labels
area/documentation Indicates an issue on documentation area. kind/bug Indicates an issue kind/missing-implementation

Comments

@rohan-97
Copy link

Actual Behavior

Getting generic resposne on validation error

{
    "errors": [
        {
            "class": "<class 'openapi_core.validation.schemas.exceptions.InvalidSchemaValue'>",
            "status": 400,
            "title": "Value {'flag': 'testF'} not valid for schema of type object: (<ValidationError: \"'testF' is too short\">,)"
        }
    ]
}

Expected Behavior

We should be able to generate custom responses, and should be able to capture the validation error info e.g. status code, validation error message, and title

Steps to Reproduce

I set up a flask server and followed the instructions provided here

Currently openapi-core is validating the incoming requets properly, however the response generated is as follows

{
    "errors": [
        {
            "class": "<class 'openapi_core.validation.schemas.exceptions.InvalidSchemaValue'>",
            "status": 400,
            "title": "Value {'flag': 'testF'} not valid for schema of type object: (<ValidationError: \"'testF' is too short\">,)"
        }
    ]
}

I want to customize the response body as per my application,

I wanted to know if is there any provision using which we can provide custom responses.

or even if we somehow capture the validation error info e.g. status code, validation error message, and title then we can generate our own response with the available information.

Thanks in advance

OpenAPI Core Version

0.17.1

OpenAPI Core Integration

Flask

Affected Area(s)

No response

References

No response

Anything else we need to know?

No response

Would you like to implement a fix?

None

@rohan-97 rohan-97 added the kind/bug Indicates an issue label Apr 27, 2023
@p1c2u
Copy link
Collaborator

p1c2u commented Apr 28, 2023

Hi @rohan-97

thanks for the report. Errors handling customization is one of the topics missing in current documentation (I didn't add this to the documentation because of missing well-defined interface and possible interface changes in near future). Currently it's possible customize errors handling by passing your own handler class with openapi_errors_handler parameter (See decorator and default errors handler). So most likely you would want to inherit from FlaskOpenAPIErrorsHandler class.

@p1c2u p1c2u added kind/missing-implementation area/documentation Indicates an issue on documentation area. labels Apr 28, 2023
@rohan-97
Copy link
Author

rohan-97 commented Apr 28, 2023

Thanks @p1c2u for the quick response,
I was able to customize the response by inheriting FlaskOpenAPIErrorsHandler class.

I believe this is really nice feature to have, and should be documented,
I'll keep this issue open so that this can be documented in future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/documentation Indicates an issue on documentation area. kind/bug Indicates an issue kind/missing-implementation
Projects
None yet
Development

No branches or pull requests

2 participants