Deprecating BaseHTTPMiddleware
See original GitHub issueBased on many previous discussions, I am proposing that we deprecate BaseHTTPMiddleware
.
We’ll put a deprecation warning in the constructor. In this warning we will give:
- Some background on the issue
- A date at which we will remove
BaseHTTPMiddleware
- Options for developers (basically help us fix it or migrate to ASGI middleware).
This will be a part of a multipronged approach consisting of:
- ~Better documentation. We’ve already documented the limitations and issues with
BaseHTTPMiddleware
both in our docs and tests. #1656 will give us better documentation on creating ASGI middleware.~ This is now done! - ~I (and I think @Kludex as well) are prepared to personally contribute to downstream packages by porting their middlewares to pure ASGI middleware. We’ll probably just look for any packages with > X stars or something and make PRs.~ We’ve proposed (and merged some) upstream fixes into some of the most popular libraries using
BaseHTTPMiddleware
we could find on GitHub. - Providing an alternative implementation. #1691 proposes an alternative safer implementation of a simple request/response middleware. I’m not sure this is necessary now that we have better ASGI middleware docs, but it’s an option.
- Deprecating and eventually removing BaseHTTPMiddleware
Issue Analytics
- State:
- Created a year ago
- Reactions:11
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Deprecating BaseHTTPMiddleware - - Bountysource
Based on many previous discussions, I am proposing that we deprecate BaseHTTPMiddleware . We'll put a deprecation warning in the constructor.
Read more >Middleware — Starlette Context 0.3.5 documentation
ContextMiddleware middleware is deprecated and will be removed in version 1.0.0 ... ContextMiddleware inherits from BaseHTTPMiddleware which is an interface ...
Read more >Release Notes - Starlette
Deprecated. Deprecate Starlette and Router decorators #1897. ... Do not send empty data to StreamingResponse on BaseHTTPMiddleware #1609.
Read more >tiangolo/fastapi - Gitter
you can check BaseHTTPMiddleware in starlette. Dmitriy Geels. @dmig-alarstudios. @euri10 sounds like a plan for my question. euri10. @euri10.
Read more >starlette Changelog - pyup.io
Deprecate `Starlette` and `Router` decorators ... Fix bug on which `BackgroundTasks` were cancelled when using `BaseHTTPMiddleware` and client disconnected ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is a rather bold move to make given the usage of BaseHTTPMiddleware in the wild, so let’s pause and think it through. I’m sure there’s been discussion before (are there any links available?), but jotting down some thoughts and questions…
In general, what’s the migration path going to be like for users of BaseHTTPMiddleware? Should we start by exploring how FastAPI could do without? It’s still relying on
@app.middleware
extensively. If the hundreds of thousands (maybe?) of FastAPI users get this deprecation warning, what action should they take to keep their systems running? Certainly the action might be on some intermediate dependency maintenance team. Does this mean we should first help folks migrate, then deprecate?What does addressing the two problems mentioned in the docs (context vars, background tasks) look like with pure ASGI middleware?
What do existing examples in the BaseHTTPMiddleware docs look like using pure ASGI middleware?
While I reckon the limitations of BaseHTTPMiddleware, and the fact that currently users expect certain things to work that don’t, I also fear « pure ASGI middleware all the things » might be really off putting a lot of folks who’d like to do very simple edits to requests or responses.
Now, if we consider something like #1691, I think a migration path could be:
BaseHTTPMiddleware
eventually?) a deprecation warning hinting users to switch to that or pure ASGI middleware. #1691 makes it so that the transformation fromBaseHTTPMiddleware
style toHTTPMiddleware
style is fairly straightforward – basically(request, call_next)
->(conn)
andresponse = await call_next(request)
->response = yield
. If this falls on intermediate libraries, end-users can always-W
ignore this particular deprecation for a while.BaseHTTPMiddleware
. (I think this criteria is better than committing to a specific date, even in the form of “First minor bump of 2023”. If folks want to prepare, they can’t know what minor that’s going to be.)?