Tornado-JSON icon indicating copy to clipboard operation
Tornado-JSON copied to clipboard

async handler methods not supported (Tornado 6.x)

Open ricogini opened this issue 6 years ago • 2 comments

A serialization exception is thrown if a handler is async instead of decorated with @coroutine Example:

class MyFailingHandler(APIHandler):

  @schema.validate(input_schema={'type': 'string'})
  async def post(self):
    self.set_status(201, "created")

Thrown error:

File "/home/.virtualenvs/annotation_tool/lib/python3.6/site-packages/tornado/web.py", line 1592, in _execute
    result = yield result
  File "/home/.virtualenvs/annotation_tool/lib/python3.6/site-packages/tornado/gen.py", line 1133, in run
    value = future.result()
  File "/home/.virtualenvs/annotation_tool/lib/python3.6/site-packages/tornado/gen.py", line 326, in wrapper
    yielded = next(result)
  File "/home/.virtualenvs/annotation_tool/lib/python3.6/site-packages/tornado_json/schema.py", line 193, in _wrapper
    self.success(output)
  File "/home/.virtualenvs/annotation_tool/lib/python3.6/site-packages/tornado_json/jsend.py", line 20, in success
    self.write({'status': 'success', 'data': data})
  File "/home/.virtualenvs/annotation_tool/lib/python3.6/site-packages/tornado/web.py", line 746, in write
    chunk = escape.json_encode(chunk)
  File "/home/.virtualenvs/annotation_tool/lib/python3.6/site-packages/tornado/escape.py", line 81, in json_encode
    return json.dumps(value).replace("</", "<\\/")
  File "/usr/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'coroutine' is not JSON serializable

Tested with python 3.6

ricogini avatar Oct 24 '18 14:10 ricogini

I got hit by the same on tornado 6.0.x and python 3.7.1.

pletnes avatar Apr 16 '19 13:04 pletnes

Hey folks, looks like this is a breaking change in Tornado 6.x. For the time being, I have created a new release (1.3.4) that pins a working set of dependencies so the project continues to behave as it has before.

I don't have time to look into updating Tornado-JSON to support Tornado 6.x at the moment, but I welcome PRs in case anyone is interested in adding support.

hfaran avatar Apr 17 '19 10:04 hfaran