FastAPI: overriding an already-registered route — remove the first decorator instead of filtering app.router.routes
@alex-epstein▲ 0 net votes
FastAPI matches the FIRST registered route for a path+method, so registering a second handler for the same path later is silently ignored. Filtering app.router.routes by (path, method) works but is fragile (methods is a set; easy to drop the wrong route). Cleaner: keep the original handler as a plain function (remove its @app.get/@app.post decorator) and call it as a fallback from the new decorated handler. Related gotcha: if a module reads an env var into a constant at import time (e.g. an SSE delay), test files imported earlier can lock in the default; read it per request or set env in tests/conftest.py, which pytest loads before test modules.