Override a FastAPI dependency at the APIRouter level
Tests FastAPI DI scoping limits. Answer: APIRouter has no dependency_overrides; create a sub-app, apply overrides, mount it. Red flag: Claiming router-level overrides exist or mutating global app state.
WHAT THIS TESTS: This question probes whether you understand the boundaries of FastAPI's dependency injection container. Specifically, it checks if you know that APIRouter is only a route grouping mechanism and does not possess its own dependency override registry, and whether you can apply the sub-application pattern to achieve scoped mocking without side effects.
A GOOD ANSWER COVERS: A strong answer starts by stating clearly that APIRouter lacks a dependency_overrides attribute; only FastAPI application instances maintain this dictionary. Next, it describes the sub-application pattern: instantiate a new FastAPI object, assign the desired mock to its dependency_overrides dictionary using the original dependency callable as the key, include the APIRouter into this sub-application, and finally mount the sub-application under a path prefix in the main application using app.mount. The candidate should explain that because the mounted sub-application runs with its own DI container, requests hitting that router use the mocked dependency while every other route in the main application continues to use the original or globally overridden version. It is also worth mentioning that this approach is idiomatic for testing isolated route groups and avoids temporal coupling or state leakage.
COMMON WRONG ANSWERS: The biggest red flag is claiming you can set router.dependency_overrides directly. Another frequent mistake is suggesting that you temporarily mutate app.dependency_overrides inside a test setup and then revert it after, which introduces race conditions and affects the entire application. Some candidates propose monkey-patching the underlying dependency function at the module level, bypassing FastAPI's injection system entirely. A weaker answer suggests adding conditional logic inside the dependency function to check the request path, which conflates business logic with routing infrastructure.
LIKELY FOLLOW-UPS: An interviewer might ask how mounting affects the OpenAPI schema, since each mounted sub-application generates its own independent schema by default. They could also ask about the performance cost of multiple FastAPI instances in a single process, or how you would propagate shared middleware, exception handlers, or lifespan events from the main app to the sub-app. Another angle is testing: how would you write tests that target only the sub-application without spinning up the entire main app.
ONE CONCRETE EXAMPLE: Imagine a main application where app.dependency_overrides overrides get_db to return a pooled connection. A billing router, however, must be tested with an in-memory SQLite database. You would create billing_app = FastAPI(), then set billing_app.dependency_overrides[get_db] = get_memory_db. After including the billing router in billing_app, you mount it with app.mount("/billing", billing_app). Calls to /billing/invoices now resolve get_db to the in-memory version, while /users/profile continues using the pooled connection override defined on the main app.
Read the original → fastapi.tiangolo.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.