Path Parameters in Advanced Dependency
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
@router.get("/{project_id}/team}", response_model=List[schemas.team])
def get_team(
project_id: UUID,
is_authorized: bool = Depends(deps.AuthorizationCheck(project_id=project_id))
) -> Any:
# Get the Team through crud operations
pass
class AuthorizationCheck:
def __init__(self, project_id) -> None:
self.project_id = project_id
def __call__(self, air_data_session: Session = Depends(session),
auth_id: str = Depends(get_firebase_auth_id)) -> bool:
if not crud.user.is_user_part_of_module_and_project(
session,
project_id=str(self.project_id),
firebase_auth_id=auth_id,
):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="User not part of project",
)
return True
Description
I’m implementing a dependency that checks if the user is allowed to access a certain project or not. If they are not allowed, the dependency will raise a 401 exception otherwise, it will return true.
The project_id is provided through the path parameter and several APIs will be using this dependency. Here’s an example of one of the API that is supposed to fetch the team working on a specific project api/v2/{project_id}/team
As an attempt, I created a dependency class called AuthorizationCheck that takes as input project_id. However, project_id is not being recognized from the path parameter.
I’ve read the Advanced Dependency tutorial provided in the documentation but it doesn’t mention a way to use the path parameters in it. Is this doable?
Operating System
Windows
Operating System Details
No response
FastAPI Version
0.54.2
Python Version
3.9.6
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
@frankie567 Merci beaucoup!! It’s exactly what I wanted 🎉
The problem here is that you are using a value that will be available at runtime,
project_id
, at “compile time”:Actually, all you have to do is to change your
__call__
method so that it injectsproject_id
. That’s the beauty of FastAPI: dependencies are 100% recursive:If you don’t need to manage other parameters, a class may be unnecessary, you can do it with a simple function:
Even better, if you don’t need the return value, you can just inject this dependency in the route decorator:
You can reuse this dependency at will. The only thing you have to take care of is to put the
project_id
parameter in your path pattern. Otherwise, FastAPI will automatically look for it in query parameters.