|
1 | 1 | from typing import Dict, Optional |
2 | 2 |
|
3 | | -from fastapi import APIRouter, Depends, FastAPI, Header, status |
| 3 | +from fastapi import APIRouter, Depends, FastAPI, Header, Path, status |
4 | 4 | from httpx import AsyncClient |
5 | 5 | from pydantic import BaseModel |
6 | 6 | from pytest import mark # noqa |
|
17 | 17 | ) |
18 | 18 | from fastapi_jsonapi.views.view_base import ViewBase |
19 | 19 | from tests.fixtures.db_connection import async_session_dependency |
20 | | -from tests.fixtures.views import SessionDependency, common_handler |
| 20 | +from tests.fixtures.views import SessionDependency |
21 | 21 | from tests.misc.utils import fake |
22 | 22 | from tests.models import User |
23 | 23 | from tests.schemas import ( |
@@ -131,12 +131,23 @@ async def check_that_user_is_admin(x_auth: Annotated[str, Header()]): |
131 | 131 | class AdminOnlyPermission(BaseModel): |
132 | 132 | is_admin: Optional[bool] = Depends(check_that_user_is_admin) |
133 | 133 |
|
| 134 | + def get_path_obj_id(obj_id: int = Path(default=...)): |
| 135 | + return obj_id |
| 136 | + |
| 137 | + class DetailGenericDependency(SessionDependency): |
| 138 | + custom_name_obj_id: int = Depends(get_path_obj_id) |
| 139 | + |
| 140 | + def all_handler(view: ViewBase, dto: DetailGenericDependency) -> Dict: |
| 141 | + # test inside handler |
| 142 | + assert dto.custom_name_obj_id == int(view.request.path_params["obj_id"]) |
| 143 | + return {"session": dto.session} |
| 144 | + |
134 | 145 | class DependencyInjectionDetailView(DetailViewBaseGeneric): |
135 | 146 | method_dependencies: Dict[HTTPMethod, HTTPMethodConfig] = { |
136 | 147 | HTTPMethod.GET: HTTPMethodConfig(dependencies=AdminOnlyPermission), |
137 | 148 | HTTPMethod.ALL: HTTPMethodConfig( |
138 | | - dependencies=SessionDependency, |
139 | | - prepare_data_layer_kwargs=common_handler, |
| 149 | + dependencies=DetailGenericDependency, |
| 150 | + prepare_data_layer_kwargs=all_handler, |
140 | 151 | ), |
141 | 152 | } |
142 | 153 |
|
|
0 commit comments