connexion icon indicating copy to clipboard operation
connexion copied to clipboard

Move functionality into pluggable ASGI middleware stack

Open RobbeSneyders opened this issue 2 years ago • 0 comments

Currently, Connexion works by injecting its functionality in between the underlying framework app and the user view function, by wrapping the view function in decorators. (See our ARCHITECTURE.rst).

We propose to move most of this functionality to ASGI middleware, which is wrapped around the framework app. This allows this functionality to be framework agnostic, and actually work with any ASGI (or WSGI by using an adapter) compliant framework. This means that you can add the power of Connexion to existing apps of any of these frameworks, without Connexion having to maintain a framework-specific interface.

image The only functionalities that cannot be moved to this framework agnostic middleware are the resolver to automatically map operations to python functions, and the automatic parameter unpacking which needs to remain a decorator since it needs to have access to the view function.

Originally posted in https://github.com/spec-first/connexion/issues/1395#issuecomment-1063214142

This also makes the functionality pluggable so built-in features can easily be disabled and additional functionality can easily be added.

I've implemented a proof of concept that shows this middleware concept in https://github.com/spec-first/connexion/pull/1477.

The goal is to:

  1. Make the middleware separately available for usage with any ASGI / WSGI app
from connexion import ConnexionMiddleware
from framework import App

app = App()
app = ConnexionMiddleware(app, __name__, specification_dir='openapi/')
app.add_api('my_api.yaml')
  1. Use it within the Connexion App so we can keep offering the current interface
from connexion import App

app = App(__name__, specification_dir='openapi/')
app.add_api('my_api.yaml')

Tasks

  • [x] Create ConnexionMiddleware #1492
  • [x] Extract SwaggerUI functionality into middleware #1496
  • [x] Add routing middleware #1497
  • [x] Extract security into middleware #1514
  • [ ] Extract (de)serialization into middleware #1524
  • [ ] Extract validation into middleware #1525

RobbeSneyders avatar Mar 17 '22 19:03 RobbeSneyders