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
Allow to order the middleware list #1490
Comments
The middleware order is mentioned on our docs: https://www.starlette.io/middleware/ |
@Kludex @tomchristie I don't think this issue should be closed, I believe you are misunderstanding what @sylvainmouquet is raising an issue to. If we applied the same middleware via the middleware = [
Middleware(
TrustedHostMiddleware,
allowed_hosts=['example.com', '*.example.com'],
),
Middleware(HTTPSRedirectMiddleware),
Middleware(CORSMiddleware, allow_origins=['*'])
]
print("Middleware list:\n", middleware)
print()
app = Starlette(middleware=middleware)
app.user_middleware
print("app.user_middleware after passing list into Starlette constructor: ", app.user_middleware)
print()
app = Starlette()
for mid_type, opts in middleware:
app.add_middleware(mid_type, **opts)
print("app.user_middleware after iterating through list and adding middleware via app.add_middleware: ", app.user_middleware) Output:
As you can see, depending on how the middleware is added, the order changes. IMO, it makes intuitive sense to go the way of the constructor, as the order the middleware is passed in is the order that starlette will evaluate it. Futhermore, FastAPI only provides users the option to add middleware via the I think the issue should be changed from "Allow to order the middleware list" to unifying the two methods so that both the constructor and the Let me know your thoughts! I could also take a stab at implementing this if you'd like. |
We are aware. There have been discussions to deprecate the |
I see. Would it be better to deprecate the method vs. altering the behavior in a breaking change? |
Checklist
Is your feature related to a problem? Please describe.
The adding of a new middleware is done after the first position.
There is not a method to reordered the list of middleware
Describe the solution you would like.
Add q
Describe alternatives you considered
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: