spec-first/connexion

FlaskApp server not working with uvicorn reload option enabled

kakuche opened this issue · 1 comments

Description

When trying to run a development server for a Flask api the uvicorn reloader appears to duplicate the api route definitions, causing the error shown below. The server runs normally as long as the automatic reloader is disabled.

Since this is my first attempt at api development there's a good chance I simply missed some necessary settings, so any feedback is appreciated.

Expected behaviour

Update of the development server on code changes to .py files.

Actual behaviour

Running the development server with uvicorn and reload=True argument causes the following error on startup:

AssertionError: View function mapping is overwriting an existing endpoint function: index

This triggers either on the first route defined with an @app.route() decorator, or the server url in the openapi specification when no additional routes are defined with the decorator.

Steps to reproduce

install connexion with pip install connexion[flask,uvicorn,swagger-ui]

create app as follows and attempt to run

import pathlib
from connexion import App

basedir = pathlib.Path(__file__).parent.resolve()
app = App(__name__, specification_dir=basedir)
app.add_api(basedir / "openapi.yml")

@app.route("/")
def index():
    return "something"

if __name__ == "__main__":

    app.run(f"{Path(__file__).stem}:app", host="127.0.0.1", port=8000, reload=True)

Additional info:

Output of the commands:

  • python --version: Python 3.11.5
  • pip show connexion | grep "^Version\:": Version 3.0.6

Hi @kakuche

If the index route is defined in your openapi.yml file, you don't need to register it as a route (using @app.route) yourself anymore. Connexion automatically registers all the routes in your API specification.