0
0
FastAPIframework~20 mins

Shared dependencies across routers in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Dependency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing the endpoint with shared dependency?

Consider a FastAPI app with two routers sharing a dependency that returns a user ID. What will the response be when calling /items/?

FastAPI
from fastapi import FastAPI, Depends, APIRouter

app = FastAPI()

async def get_user_id():
    return "user123"

router1 = APIRouter(dependencies=[Depends(get_user_id)])

@router1.get("/items/")
async def read_items():
    return {"message": "Items accessed"}

router2 = APIRouter(dependencies=[Depends(get_user_id)])

@router2.get("/users/")
async def read_users():
    return {"message": "Users accessed"}

app.include_router(router1)
app.include_router(router2)
A{"detail": "Not Found"}
B{"message": "Users accessed"}
CRuntimeError due to dependency conflict
D{"message": "Items accessed"}
Attempts:
2 left
💡 Hint

Think about which router handles the /items/ path and what the shared dependency returns.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies a shared dependency to multiple routers?

Given two routers, which code snippet correctly applies the same dependency to both routers?

FastAPI
from fastapi import APIRouter, Depends

async def common_dep():
    return "common"

router1 = APIRouter()
router2 = APIRouter()

# Which option correctly adds common_dep as a shared dependency?
A
router1 = APIRouter(dependencies=[Depends(common_dep)])
router2 = APIRouter(dependencies=[Depends(common_dep)])
B
router1.dependencies = [Depends(common_dep)]
router2.dependencies = [Depends(common_dep)]
C
router1 = APIRouter()
router2 = APIRouter()
router1.include_router(router2, dependencies=[Depends(common_dep)])
D
router1 = APIRouter(dependencies=Depends(common_dep))
router2 = APIRouter(dependencies=Depends(common_dep))
Attempts:
2 left
💡 Hint

Check how dependencies are passed as a list to the APIRouter constructor.

🔧 Debug
advanced
2:00remaining
Why does this shared dependency raise a runtime error?

Examine the code below. When running the app, a runtime error occurs related to the shared dependency. What causes this error?

FastAPI
from fastapi import FastAPI, Depends, APIRouter

app = FastAPI()

async def get_db():
    raise RuntimeError("DB connection failed")

router = APIRouter(dependencies=[Depends(get_db)])

@router.get("/data")
async def read_data():
    return {"data": "sample"}

app.include_router(router)
AThe dependency is not awaited properly, causing a coroutine warning.
BThe router is missing a prefix, causing routing errors.
CThe dependency get_db raises an exception, causing the endpoint to fail.
DFastAPI does not support async dependencies in routers.
Attempts:
2 left
💡 Hint

Look at what the dependency function does when called.

state_output
advanced
2:00remaining
What is the value of the shared dependency parameter inside the endpoint?

Given this FastAPI router with a shared dependency that returns a string, what is the value of user inside the endpoint?

FastAPI
from fastapi import APIRouter, Depends

async def get_current_user():
    return "alice"

router = APIRouter(dependencies=[Depends(get_current_user)])

@router.get("/profile")
async def profile(user: str = Depends(get_current_user)):
    return {"user": user}
ANone
B"alice"
CRaises a TypeError due to duplicate dependency
D"user"
Attempts:
2 left
💡 Hint

Consider how FastAPI resolves dependencies when declared both in router and endpoint.

🧠 Conceptual
expert
2:00remaining
Why use shared dependencies at router level instead of endpoint level?

Which is the main advantage of applying dependencies at the router level instead of individually on each endpoint?

AIt ensures the dependency is executed once per request for all endpoints in the router, reducing code repetition.
BIt disables dependency injection for endpoints not needing it.
CIt automatically caches the dependency result across all requests.
DIt allows dependencies to modify the router's URL prefix dynamically.
Attempts:
2 left
💡 Hint

Think about code reuse and how dependencies apply to multiple endpoints.