Async/await in Flask 2.0+ breaks due to decorator order
phil-chp opened this issue · 1 comments
Name | Version |
---|---|
Flasgger | 0.9.7.1 |
Flask | 2.3.2 |
Python | 3.9, 3.10 |
OS | macOS 13.3 |
I'm setting up a basic Flask project with an asynchronous route. I want to fetch information online using the selenium
package, and this requires the Flask route to await for the task to finish.
I've been using @swag_from()
decorators to document my code and make it easier to work with, here is an idea of what my code looked like:
@website_api.route('/get-data-from-page', methods=['POST'])
@swag_from('swagger/get-data-from-page.yml')
async def get_data_from_page():
data = req.get_json()
res = await common.get_data_from_page(data["url"])
return json.dumps({ "data": res }), 201
For some reason, this kept throwing: TypeError: The view function did not return a valid response. The return type must be a string, dict, list, tuple with headers or status, Response instance, or WSGI callable, but it was a coroutine.
After a couple of hours of trying everything, re-installing Python and both libraries, I found the issue.
This causes the error:
@app.route(<route>)
@swag_from(<file>)
async def route():
# ...
This doesn't:
@swag_from(<file>)
@app.route(<route>)
async def route():
# ...
(Note the order of the decorators)
Now, this might not be a bug at all, it even might not have anything to do with flasgger and could be a problem is Flask directly. But the README of this project says to do it like so:
@app.route('/colors/<palette>/')
@swag_from(specs_dict)
def colors(palette):
# ...
Maybe a switch between those two lines in the README and any documentation would be interesting?