Skip to content

Commit d0706b4

Browse files
committed
fix atomic operations method config resolution
1 parent ec13807 commit d0706b4

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

fastapi_jsonapi/api.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from fastapi_jsonapi.schema_base import BaseModel
2323
from fastapi_jsonapi.schema_builder import SchemaBuilder
2424
from fastapi_jsonapi.signature import create_additional_query_params
25+
from fastapi_jsonapi.utils.dependency_helper import DependencyHelper
2526
from fastapi_jsonapi.views.utils import (
2627
HTTPMethod,
2728
HTTPMethodConfig,
@@ -408,13 +409,6 @@ def _update_method_config_and_get_dependency_params(
408409

409410
return self._create_dependency_params_from_pydantic_model(method_config.dependencies)
410411

411-
def get_method_config_for_create(self):
412-
method_config = self._update_method_config(
413-
view=self.list_view_resource,
414-
method=HTTPMethod.POST,
415-
)
416-
return method_config
417-
418412
def prepare_dependencies_handler_signature(
419413
self,
420414
custom_handler: Callable[..., Any],
@@ -432,6 +426,35 @@ def prepare_dependencies_handler_signature(
432426

433427
return sig.replace(parameters=params + list(additional_dependency_params) + tail_params)
434428

429+
async def handle_view_dependencies(
430+
self,
431+
request: Request,
432+
view_cls: Type["ViewBase"],
433+
method: HTTPMethod,
434+
) -> Dict[str, Any]:
435+
"""
436+
Consider method config is already prepared for generic views
437+
Reuse the same config for atomic operations
438+
439+
:param request:
440+
:param view_cls:
441+
:param method:
442+
:return:
443+
"""
444+
method_config: HTTPMethodConfig = view_cls.method_dependencies[method]
445+
446+
def handle_dependencies(**dep_kwargs):
447+
return dep_kwargs
448+
449+
handle_dependencies.__signature__ = self.prepare_dependencies_handler_signature(
450+
custom_handler=handle_dependencies,
451+
method_config=method_config,
452+
)
453+
454+
dep_helper = DependencyHelper(request=request)
455+
dependencies_result: Dict[str, Any] = await dep_helper.run(handle_dependencies)
456+
return dependencies_result
457+
435458
def _create_get_resource_list_view(self):
436459
"""
437460
Create wrapper for GET list (get objects list)

fastapi_jsonapi/atomic/atomic_handler.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
Any,
99
Awaitable,
1010
Callable,
11-
Dict,
1211
List,
1312
Optional,
1413
TypedDict,
@@ -22,8 +21,6 @@
2221
from fastapi_jsonapi import RoutersJSONAPI
2322
from fastapi_jsonapi.atomic.prepared_atomic_operation import LocalIdsType, OperationBase
2423
from fastapi_jsonapi.atomic.schemas import AtomicOperation, AtomicOperationRequest, AtomicResultResponse
25-
from fastapi_jsonapi.utils.dependency_helper import DependencyHelper
26-
from fastapi_jsonapi.views.utils import HTTPMethodConfig
2724

2825
if TYPE_CHECKING:
2926
from fastapi_jsonapi.data_layers.base import BaseDataLayer
@@ -73,23 +70,6 @@ def __init__(
7370
self.operations_request = operations_request
7471
self.local_ids_cache: LocalIdsType = defaultdict(dict)
7572

76-
async def handle_view_dependencies(
77-
self,
78-
jsonapi: RoutersJSONAPI,
79-
) -> Dict[str, Any]:
80-
method_config: HTTPMethodConfig = jsonapi.get_method_config_for_create()
81-
82-
def handle_dependencies(**dep_kwargs):
83-
return dep_kwargs
84-
85-
handle_dependencies.__signature__ = jsonapi.prepare_dependencies_handler_signature(
86-
custom_handler=handle_dependencies,
87-
method_config=method_config,
88-
)
89-
90-
dependencies_result: Dict[str, Any] = await DependencyHelper(request=self.request).run(handle_dependencies)
91-
return dependencies_result
92-
9373
async def prepare_one_operation(self, operation: AtomicOperation):
9474
"""
9575
:param operation:
@@ -102,16 +82,12 @@ async def prepare_one_operation(self, operation: AtomicOperation):
10282
raise ValueError(msg)
10383
jsonapi = RoutersJSONAPI.all_jsonapi_routers[operation_type]
10484

105-
dependencies_result: Dict[str, Any] = await self.handle_view_dependencies(
106-
jsonapi=jsonapi,
107-
)
108-
one_operation = OperationBase.prepare(
85+
one_operation = await OperationBase.prepare(
10986
action=operation.op,
11087
request=self.request,
11188
jsonapi=jsonapi,
11289
ref=operation.ref,
11390
data=operation.data,
114-
data_layer_view_dependencies=dependencies_result,
11591
)
11692
return one_operation
11793

fastapi_jsonapi/atomic/prepared_atomic_operation.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from fastapi_jsonapi import RoutersJSONAPI
99
from fastapi_jsonapi.atomic.schemas import AtomicOperationAction, AtomicOperationRef, OperationDataType
10+
from fastapi_jsonapi.views.utils import HTTPMethod
1011

1112
if TYPE_CHECKING:
1213
from fastapi_jsonapi.data_layers.base import BaseDataLayer
@@ -27,14 +28,13 @@ class OperationBase:
2728
op_type: str
2829

2930
@classmethod
30-
def prepare(
31+
async def prepare(
3132
cls,
3233
action: str,
3334
request: Request,
3435
jsonapi: RoutersJSONAPI,
3536
ref: Optional[AtomicOperationRef],
3637
data: OperationDataType,
37-
data_layer_view_dependencies: Dict[str, Any],
3838
) -> "OperationBase":
3939
view_cls: Type[ViewBase] = jsonapi.detail_view_resource
4040

@@ -55,6 +55,11 @@ def prepare(
5555

5656
view = view_cls(request=request, jsonapi=jsonapi)
5757

58+
data_layer_view_dependencies: Dict[str, Any] = await jsonapi.handle_view_dependencies(
59+
request=request,
60+
view_cls=view_cls,
61+
method=operation_cls.http_method,
62+
)
5863
return operation_cls(
5964
jsonapi=jsonapi,
6065
view=view,
@@ -127,6 +132,8 @@ class DetailOperationBase(OperationBase):
127132

128133

129134
class OperationAdd(ListOperationBase):
135+
http_method = HTTPMethod.POST
136+
130137
async def handle(self, dl: BaseDataLayer):
131138
data_in = self.jsonapi.schema_in_post(data=self.data)
132139
response = await self.view.process_create_object(
@@ -137,6 +144,8 @@ async def handle(self, dl: BaseDataLayer):
137144

138145

139146
class OperationUpdate(DetailOperationBase):
147+
http_method = HTTPMethod.PATCH
148+
140149
async def handle(self, dl: BaseDataLayer):
141150
if self.data is None:
142151
# TODO: clear to-one relationships
@@ -153,6 +162,8 @@ async def handle(self, dl: BaseDataLayer):
153162

154163

155164
class OperationRemove(DetailOperationBase):
165+
http_method = HTTPMethod.DELETE
166+
156167
async def handle(
157168
self,
158169
dl: BaseDataLayer,

0 commit comments

Comments
 (0)