Skip to content
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

Support dependency overrides on the versioned app #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fastapi_versioning/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def VersionedFastAPI(
title=app.title,
**kwargs,
)
parent_app.dependency_overrides = app.dependency_overrides
version_route_mapping: Dict[Tuple[int, int], List[APIRoute]] = defaultdict(
list
)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_dependency_overrides.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from fastapi import APIRouter, Depends, FastAPI
from fastapi.testclient import TestClient
from fastapi_versioning import versioned_api_route, VersionedFastAPI

original_app = FastAPI(title="test-app")
router = APIRouter(route_class=versioned_api_route(1, 0))

def dependency() -> str:
return "original"

@router.get("/test")
def get_test(dep: str = Depends(dependency)) -> str:
return dep

original_app.include_router(router)
app = VersionedFastAPI(original_app)


def test_dependency_override_works() -> None:
client = TestClient(app)
app.dependency_overrides[dependency] = lambda: "patched"
assert client.get("/v1_0/test").json() == "patched", "Dependency override doesn't work"