Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.7k
🐛 Cache dependencies that don't use scopes and don't have sub-dependencies with scopes#14419
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
+195 −20
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
2285bbc 🧪 Add test for not duplicating dependencies without scopes
tiangoloc01f9e5 🧪 Simplify test
tiangolobf78eb7 🧪 Add extra tests for security scopes
tiangolo81fdd39 🧪 Update test scopes
tiangolo8381fcf 🐛 Cache dependencies that don't use scopes and don't have sub-depende…
tiangolo11a7e62 🎨 Auto format
github-actions[bot]df8f5cd ✅ Fix tests for Python 3.8
tiangoloFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
28 changes: 26 additions & 2 deletionsfastapi/dependencies/models.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
34 changes: 16 additions & 18 deletionsfastapi/dependencies/utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletionstests/test_security_scopes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from typing import Dict | ||
| import pytest | ||
| from fastapi import Depends, FastAPI, Security | ||
| from fastapi.testclient import TestClient | ||
| from typing_extensions import Annotated | ||
| @pytest.fixture(name="call_counter") | ||
| def call_counter_fixture(): | ||
| return {"count": 0} | ||
| @pytest.fixture(name="app") | ||
| def app_fixture(call_counter: Dict[str, int]): | ||
| def get_db(): | ||
| call_counter["count"] += 1 | ||
| return f"db_{call_counter['count']}" | ||
| def get_user(db: Annotated[str, Depends(get_db)]): | ||
| return "user" | ||
| app = FastAPI() | ||
| @app.get("/") | ||
| def endpoint( | ||
| db: Annotated[str, Depends(get_db)], | ||
| user: Annotated[str, Security(get_user, scopes=["read"])], | ||
| ): | ||
| return {"db": db} | ||
| return app | ||
| @pytest.fixture(name="client") | ||
| def client_fixture(app: FastAPI): | ||
| return TestClient(app) | ||
| def test_security_scopes_dependency_called_once( | ||
| client: TestClient, call_counter: Dict[str, int] | ||
| ): | ||
| response = client.get("/") | ||
| assert response.status_code == 200 | ||
| assert call_counter["count"] == 1 |
107 changes: 107 additions & 0 deletionstests/test_security_scopes_sub_dependency.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Ref: https://github.com/fastapi/fastapi/discussions/6024#discussioncomment-8541913 | ||
| from typing import Dict | ||
| import pytest | ||
| from fastapi import Depends, FastAPI, Security | ||
| from fastapi.security import SecurityScopes | ||
| from fastapi.testclient import TestClient | ||
| from typing_extensions import Annotated | ||
| @pytest.fixture(name="call_counts") | ||
| def call_counts_fixture(): | ||
| return { | ||
| "get_db_session": 0, | ||
| "get_current_user": 0, | ||
| "get_user_me": 0, | ||
| "get_user_items": 0, | ||
| } | ||
| @pytest.fixture(name="app") | ||
| def app_fixture(call_counts: Dict[str, int]): | ||
| def get_db_session(): | ||
| call_counts["get_db_session"] += 1 | ||
| return f"db_session_{call_counts['get_db_session']}" | ||
| def get_current_user( | ||
| security_scopes: SecurityScopes, | ||
| db_session: Annotated[str, Depends(get_db_session)], | ||
| ): | ||
| call_counts["get_current_user"] += 1 | ||
| return { | ||
| "user": f"user_{call_counts['get_current_user']}", | ||
| "scopes": security_scopes.scopes, | ||
| "db_session": db_session, | ||
| } | ||
| def get_user_me( | ||
| current_user: Annotated[dict, Security(get_current_user, scopes=["me"])], | ||
| ): | ||
| call_counts["get_user_me"] += 1 | ||
| return { | ||
| "user_me": f"user_me_{call_counts['get_user_me']}", | ||
| "current_user": current_user, | ||
| } | ||
| def get_user_items( | ||
| user_me: Annotated[dict, Depends(get_user_me)], | ||
| ): | ||
| call_counts["get_user_items"] += 1 | ||
| return { | ||
| "user_items": f"user_items_{call_counts['get_user_items']}", | ||
| "user_me": user_me, | ||
| } | ||
| app = FastAPI() | ||
| @app.get("/") | ||
| def path_operation( | ||
| user_me: Annotated[dict, Depends(get_user_me)], | ||
| user_items: Annotated[dict, Security(get_user_items, scopes=["items"])], | ||
| ): | ||
| return { | ||
| "user_me": user_me, | ||
| "user_items": user_items, | ||
| } | ||
| return app | ||
| @pytest.fixture(name="client") | ||
| def client_fixture(app: FastAPI): | ||
| return TestClient(app) | ||
| def test_security_scopes_sub_dependency_caching( | ||
| client: TestClient, call_counts: Dict[str, int] | ||
| ): | ||
| response = client.get("/") | ||
| assert response.status_code == 200 | ||
| assert call_counts["get_db_session"] == 1 | ||
| assert call_counts["get_current_user"] == 2 | ||
| assert call_counts["get_user_me"] == 2 | ||
| assert call_counts["get_user_items"] == 1 | ||
| assert response.json() == { | ||
| "user_me": { | ||
| "user_me": "user_me_1", | ||
| "current_user": { | ||
| "user": "user_1", | ||
| "scopes": ["me"], | ||
| "db_session": "db_session_1", | ||
| }, | ||
| }, | ||
| "user_items": { | ||
| "user_items": "user_items_1", | ||
| "user_me": { | ||
| "user_me": "user_me_2", | ||
| "current_user": { | ||
| "user": "user_2", | ||
| "scopes": ["items", "me"], | ||
| "db_session": "db_session_1", | ||
| }, | ||
| }, | ||
| }, | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.