Skip to content

Commit

Permalink
Fix CORS headers not set on exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsbox committed Nov 28, 2023
1 parent bbd085b commit 55c82b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 10 additions & 0 deletions connexion/middleware/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,17 @@ def replace(self, **changes) -> "_Options":

class MiddlewarePosition(enum.Enum):
"""Positions to insert a middleware"""
BEFORE_EXCEPTION = ExceptionMiddleware
"""Add before the :class:`ExceptionMiddleware`. This is useful if you want your changes to
affect the way exceptions are handled, such as a custom error handler.
Be mindful that security has not yet been applied at this stage.
Additionally, the inserted middleware is positioned before the RoutingMiddleware, so you cannot
leverage any routing information yet and should implement your middleware to work globally
instead of on an operation level.
Usefull for CORS middleware which should be applied before the exception middleware.
"""
BEFORE_SWAGGER = SwaggerUIMiddleware
"""Add before the :class:`SwaggerUIMiddleware`. This is useful if you want your changes to
affect the Swagger UI, such as a path altering middleware that should also alter the paths
Expand Down
6 changes: 3 additions & 3 deletions docs/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Starlette. You can add it to your application, ideally in front of the ``Routing
app.add_middleware(
CORSMiddleware,
position=MiddlewarePosition.BEFORE_ROUTING,
position=MiddlewarePosition.BEFORE_EXCEPTION,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
Expand Down Expand Up @@ -62,7 +62,7 @@ Starlette. You can add it to your application, ideally in front of the ``Routing
app.add_middleware(
CORSMiddleware,
position=MiddlewarePosition.BEFORE_ROUTING,
position=MiddlewarePosition.BEFORE_EXCEPTION,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
Expand Down Expand Up @@ -96,7 +96,7 @@ Starlette. You can add it to your application, ideally in front of the ``Routing
app.add_middleware(
CORSMiddleware,
position=MiddlewarePosition.BEFORE_ROUTING,
position=MiddlewarePosition.BEFORE_EXCEPTION,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
Expand Down

0 comments on commit 55c82b1

Please sign in to comment.