flask-expects-json icon indicating copy to clipboard operation
flask-expects-json copied to clipboard

TypeError: Object of type ValidationError is not JSON serializable

Open adityapatil123 opened this issue 2 years ago • 2 comments

This library has been great help to me. Although I'm facing one issue.

I'm using flask-restx library

from flask_expects_json import expects_json
from flask_restx import Namespace, Resource, reqparse
import json

search_namespace = Namespace('search', description='Search Namespace')

@search_namespace.route("/v1/on_search")
class AddSearchCatalogues(Resource):

    @expects_json(some_schema)
    def post(self):
        return add_search_catalogues(g.data)

I'm getting this error:

jsonschema.exceptions.ValidationError: 'fulfillment_id' is a required property

Failed validating 'required' in schema['properties']['message']['properties']['catalog']['properties']['bpp/providers']['items']['properties']['items']['items']:
.
.
.
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_cors/extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_restx/api.py", line 671, in error_router
    return original_handler(f)
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_restx/api.py", line 669, in error_router
    return self.handle_error(e)
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_restx/api.py", line 766, in handle_error
    data, code, headers, fallback_mediatype=fallback_mediatype
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_restx/api.py", line 430, in make_response
    resp = self.representations[mediatype](data, *args, **kwargs)
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_restx/representations.py", line 25, in output_json
    dumped = dumps(data, **settings) + "\n"
  File "/Users/Aditya/.pyenv/versions/3.7.2/lib/python3.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/Users/Aditya/.pyenv/versions/3.7.2/lib/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/Aditya/.pyenv/versions/3.7.2/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/Users/Aditya/.pyenv/versions/3.7.2/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ValidationError is not JSON serializable

I think stringifying error object should do the work at

                return abort(400, e)

https://github.com/Fischerfredl/flask-expects-json/blob/43f8fd161ee037cdfcabb392a566a91bac01029f/flask_expects_json/init.py#L44

Is there any way we can handle this?

Thank you

adityapatil123 avatar May 01 '22 08:05 adityapatil123

Hi, great to hear that this library is useful for you.

Unfortunately I have no experience with flask-restx. The error object is passed intentionally to abort so users can do their own error handling or error formating.

The Readme has a paragraph about error handling. https://github.com/Fischerfredl/flask-expects-json/blob/master/README.md#error-handling

Does this help you?

Fischerfredl avatar May 05 '22 07:05 Fischerfredl

Oh ok I got it, so one has to implement error-handler for error-handling. Just a suggestion, we can default error-handler to avoid json-serialisation error.

Thanks a lot @Fischerfredl :smiley:

adityapatil123 avatar May 05 '22 15:05 adityapatil123