|
1 | 1 | from pathlib import Path |
2 | | -from typing import Type |
| 2 | +from typing import Optional, Type |
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 | from fastapi import APIRouter, FastAPI |
| 6 | +from pydantic import BaseModel |
6 | 7 |
|
7 | 8 | from fastapi_jsonapi import RoutersJSONAPI, init |
8 | 9 | from fastapi_jsonapi.atomic import AtomicOperations |
| 10 | +from fastapi_jsonapi.data_typing import TypeModel |
9 | 11 | from fastapi_jsonapi.views.detail_view import DetailViewBase |
10 | 12 | from fastapi_jsonapi.views.list_view import ListViewBase |
11 | 13 | from tests.fixtures.views import ( |
@@ -245,3 +247,43 @@ def build_app_custom( |
245 | 247 | app.include_router(atomic.router, prefix="") |
246 | 248 | init(app) |
247 | 249 | return app |
| 250 | + |
| 251 | + |
| 252 | +class ResourceInfoDTO(BaseModel): |
| 253 | + path: str |
| 254 | + resource_type: str |
| 255 | + model: Type[TypeModel] |
| 256 | + schema_: Type[BaseModel] |
| 257 | + schema_in_patch: Optional[BaseModel] = None |
| 258 | + schema_in_post: Optional[BaseModel] = None |
| 259 | + class_list: Type[ListViewBase] = ListViewBaseGeneric |
| 260 | + class_detail: Type[DetailViewBase] = DetailViewBaseGeneric |
| 261 | + |
| 262 | + class Config: |
| 263 | + arbitrary_types_allowed = True |
| 264 | + |
| 265 | + |
| 266 | +def build_custom_app_by_schemas(resources_info: list[ResourceInfoDTO]): |
| 267 | + router: APIRouter = APIRouter() |
| 268 | + |
| 269 | + for info in resources_info: |
| 270 | + RoutersJSONAPI( |
| 271 | + router=router, |
| 272 | + path=info.path, |
| 273 | + tags=["Misc"], |
| 274 | + class_list=info.class_list, |
| 275 | + class_detail=info.class_detail, |
| 276 | + schema=info.schema_, |
| 277 | + resource_type=info.resource_type, |
| 278 | + schema_in_patch=info.schema_in_patch, |
| 279 | + schema_in_post=info.schema_in_post, |
| 280 | + model=info.model, |
| 281 | + ) |
| 282 | + |
| 283 | + app = build_app_plain() |
| 284 | + app.include_router(router, prefix="") |
| 285 | + |
| 286 | + atomic = AtomicOperations() |
| 287 | + app.include_router(atomic.router, prefix="") |
| 288 | + init(app) |
| 289 | + return app |
0 commit comments