3

I am using Flask-RESTful for building REST api and webargs for parsing.

While defining resource, I want argument to be present, so I wrote required=True

For example:

class Name(Resource):
"""Retrieve ids corresponding to given names
Input entries: String
"""
args = {
'entries' : fields.Str(required=True),
}
@use_kwargs(args)
def get(self, entries):
    # HTTP method GET
    result = object.find_id(entries)
    return jsonify(result)

now,

While performing unittesting for API, explicitly not specifying entries, it returns Assertion Error

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1363, in handle_user_exception
assert exc_value is e
AssertionError

When I checked, exc_value is

TypeError("ValidationError({'entries': [u'Missing data for required field.']}, status_code=422, headers={}) is not JSON serializable",) 

and e is

<UnprocessableEntity '422: Unprocessable Entity'>

Now,

I've these ways to handle this exception @app.errorhandler(500)

@app.errorhandler(ValidationError)

@app.errorhandler(TypeError)

@app.errorhandler(UnprocessableEntity)

@app.errorhandler(422)

@app.errorhandler(Exception)

I don't know why even this is not working

@app.errorhandler(AssertionError)

Refered to this: link but wasn't able to solve

I am using Flask(0.10.1) Flask-Restful(0.3.5) webargs(1.2.0)

Community
  • 1
  • 1
navrocks
  • 31
  • 5

1 Answers1

2

I have hit this issue only today (even though I have been using Flask-RESTplus for almost a year now), and it came as a weird discovery. I see this issue only with Python 2.7 while Python 3.3, 3.4, 3.5 work fine.

Thus, I think it should be considered as a compatibility bug in either webargs or Flask, I'm not sure yet.

UPDATE: After some digging into the issue, I ended up in webargs and filed an issue with my troubleshooting there: https://github.com/sloria/webargs/issues/122

UPDATE 2: Here is my PR fixing this issue: https://github.com/sloria/webargs/pull/123

Vlad Frolov
  • 7,445
  • 5
  • 33
  • 52