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

Async/await in Flask 2.0+ breaks due to decorator order #586

Open
phil-chp opened this issue Jun 30, 2023 · 0 comments
Open

Async/await in Flask 2.0+ breaks due to decorator order #586

phil-chp opened this issue Jun 30, 2023 · 0 comments

Comments

@phil-chp
Copy link

phil-chp commented Jun 30, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant