Integrating Swagger/OpenAPI generated python server with existing Flask application Integrating Swagger/OpenAPI generated python server with existing Flask application flask flask

Integrating Swagger/OpenAPI generated python server with existing Flask application


It works to create the connexion.App and then extend the Flask instance from connexion.App(...).app.

It is easiest to stick with an Application Factory. In addition to being a generally useful pattern, it integrates well with the generated tests.

One gotcha is that the connexion Models seem to be expected from the controller, particularly if response validation is enabled, but they are not handled by the default JSON serializer. The model comes with a JSONEncoder class that helps with Model serialization but it needs to be connected in create_app.

def create_app():    connexionApp = connexion.App(__name__, specification_dir='swagger')    app = connexionApp.app    # This allows the connexion models to be serialized to JSON        app.json_encoder = JSONEncoder    # normal configuration    # The return value is a `connexion.Api`.    # If needed, the api blueprint is available at `connexion.Api.blueprint`    connexionApp.add_api('swagger.yaml')    return app