7
7
from marshmallow import fields , Schema
8
8
from flask_combo_jsonapi import Api
9
9
from flask_combo_jsonapi .plugin import BasePlugin
10
- from flask_combo_jsonapi .resource import ResourceList , ResourceDetail
10
+ from flask_combo_jsonapi .resource import ResourceList , ResourceDetail , Resource
11
11
from flask_combo_jsonapi .utils import SPLIT_REL
12
12
13
13
from combojsonapi .spec .apispec import DocBlueprintMixin
@@ -34,7 +34,6 @@ def __init__(self, app=None, spec_kwargs=None, decorators=None, tags: Dict[str,
34
34
self .app = None
35
35
self ._fields = []
36
36
# Use lists to enforce order
37
- self ._definitions = []
38
37
self ._fields = []
39
38
self ._converters = []
40
39
@@ -69,12 +68,6 @@ def after_init_plugin(self, *args, app=None, **kwargs):
69
68
# Register custom fields in spec
70
69
for args in self ._fields :
71
70
self .spec .register_field (* args )
72
- # Register schema definitions in spec
73
- for name , schema_cls , kwargs in self ._definitions :
74
- if APISPEC_VERSION_MAJOR < 1 :
75
- self .spec .definition (create_schema_name (schema = schema_cls ), schema = schema_cls , ** kwargs )
76
- else :
77
- self .spec .components .schema (create_schema_name (schema = schema_cls ), schema = schema_cls , ** kwargs )
78
71
# Register custom converters in spec
79
72
for args in self ._converters :
80
73
self .spec .register_converter (* args )
@@ -143,7 +136,8 @@ def param_id(self) -> dict:
143
136
"format" : "int32" ,
144
137
}
145
138
146
- def _get_operations_for_all (self , tag_name , default_parameters ) -> Dict [str , Any ]:
139
+ @classmethod
140
+ def _get_operations_for_all (cls , tag_name : str , default_parameters : list ) -> Dict [str , Any ]:
147
141
"""
148
142
Creating base dict
149
143
@@ -153,11 +147,12 @@ def _get_operations_for_all(self, tag_name, default_parameters) -> Dict[str, Any
153
147
"""
154
148
return {
155
149
"tags" : [tag_name ],
156
- "produces" : ["application/json" , ],
150
+ "produces" : ["application/json" ],
157
151
"parameters" : default_parameters if default_parameters else [],
158
152
}
159
153
160
- def __get_parameters_for_include_models (self , resource ) -> dict :
154
+ @classmethod
155
+ def __get_parameters_for_include_models (cls , resource : Resource ) -> dict :
161
156
fields_names = [
162
157
i_field_name
163
158
for i_field_name , i_field in resource .schema ._declared_fields .items ()
@@ -174,15 +169,16 @@ def __get_parameters_for_include_models(self, resource) -> dict:
174
169
"description" : f"Related relationships to include.\n Available:\n { example_models_for_include } " ,
175
170
}
176
171
177
- def __get_parameters_for_sparse_fieldsets (self , resource , description ) -> dict :
172
+ @classmethod
173
+ def __get_parameters_for_sparse_fieldsets (cls , resource : Resource , description : str ) -> dict :
178
174
# Sparse Fieldsets
179
175
return {
180
176
"name" : f"fields[{ resource .schema .Meta .type_ } ]" ,
181
177
"in" : "query" ,
182
178
"type" : "array" ,
183
179
"required" : False ,
184
180
"description" : description .format (resource .schema .Meta .type_ ),
185
- "items" : {"type" : "string" , "enum" : list (resource .schema ._declared_fields .keys ()), },
181
+ "items" : {"type" : "string" , "enum" : list (resource .schema ._declared_fields .keys ())},
186
182
}
187
183
188
184
def __get_parameters_for_declared_fields (self , resource , description ) -> Generator [dict , None , None ]:
@@ -234,7 +230,8 @@ def __list_filters_data(self) -> tuple:
234
230
},
235
231
)
236
232
237
- def __update_parameter_for_field_spec (self , new_param : dict , fld_sped : dict ) -> None :
233
+ @classmethod
234
+ def _update_parameter_for_field_spec (cls , new_param : dict , fld_sped : dict ) -> None :
238
235
"""
239
236
:param new_param:
240
237
:param fld_sped:
@@ -256,7 +253,7 @@ def __get_parameter_for_not_nested(self, field_name, field_spec) -> dict:
256
253
"required" : False ,
257
254
"description" : f"{ field_name } attribute filter" ,
258
255
}
259
- self .__update_parameter_for_field_spec (new_parameter , field_spec )
256
+ self ._update_parameter_for_field_spec (new_parameter , field_spec )
260
257
return new_parameter
261
258
262
259
def __get_parameter_for_nested_with_filtering (self , field_name , field_jsonb_name , field_jsonb_spec ):
@@ -267,7 +264,7 @@ def __get_parameter_for_nested_with_filtering(self, field_name, field_jsonb_name
267
264
"required" : False ,
268
265
"description" : f"{ field_name } { SPLIT_REL } { field_jsonb_name } attribute filter" ,
269
266
}
270
- self .__update_parameter_for_field_spec (new_parameter , field_jsonb_spec )
267
+ self ._update_parameter_for_field_spec (new_parameter , field_jsonb_spec )
271
268
return new_parameter
272
269
273
270
def __get_parameters_for_nested_with_filtering (self , field , field_name ) -> Generator [dict , None , None ]:
@@ -326,6 +323,60 @@ def _get_operations_for_get(self, resource, tag_name, default_parameters):
326
323
327
324
return operations_get
328
325
326
+ def _get_operations_for_post (self , schema : dict , tag_name : str , default_parameters : list ) -> dict :
327
+ operations = self ._get_operations_for_all (tag_name , default_parameters )
328
+ operations ["responses" ] = {
329
+ "201" : {"description" : "Created" },
330
+ "202" : {"description" : "Accepted" },
331
+ "403" : {"description" : "This implementation does not accept client-generated IDs" },
332
+ "404" : {"description" : "Not Found" },
333
+ "409" : {"description" : "Conflict" },
334
+ }
335
+ operations ["parameters" ].append (
336
+ {
337
+ "name" : "POST body" ,
338
+ "in" : "body" ,
339
+ "schema" : schema ,
340
+ "required" : True ,
341
+ "description" : f"{ tag_name } attributes" ,
342
+ }
343
+ )
344
+ return operations
345
+
346
+ def _get_operations_for_patch (self , schema : dict , tag_name : str , default_parameters : list ) -> dict :
347
+ operations = self ._get_operations_for_all (tag_name , default_parameters )
348
+ operations ["responses" ] = {
349
+ "200" : {"description" : "Success" },
350
+ "201" : {"description" : "Created" },
351
+ "204" : {"description" : "No Content" },
352
+ "403" : {"description" : "Forbidden" },
353
+ "404" : {"description" : "Not Found" },
354
+ "409" : {"description" : "Conflict" },
355
+ }
356
+ operations ["parameters" ].append (self .param_id )
357
+ operations ["parameters" ].append (
358
+ {
359
+ "name" : "POST body" ,
360
+ "in" : "body" ,
361
+ "schema" : schema ,
362
+ "required" : True ,
363
+ "description" : f"{ tag_name } attributes" ,
364
+ }
365
+ )
366
+ return operations
367
+
368
+ def _get_operations_for_delete (self , tag_name : str , default_parameters : list ) -> dict :
369
+ operations = self ._get_operations_for_all (tag_name , default_parameters )
370
+ operations ["parameters" ].append (self .param_id )
371
+ operations ["responses" ] = {
372
+ "200" : {"description" : "Success" },
373
+ "202" : {"description" : "Accepted" },
374
+ "204" : {"description" : "No Content" },
375
+ "403" : {"description" : "Forbidden" },
376
+ "404" : {"description" : "Not Found" },
377
+ }
378
+ return operations
379
+
329
380
def _add_paths_in_spec (
330
381
self ,
331
382
path : str = "" ,
@@ -372,53 +423,11 @@ def _add_paths_in_spec(
372
423
if "get" in methods :
373
424
operations ["get" ] = self ._get_operations_for_get (resource , tag_name , default_parameters )
374
425
if "post" in methods :
375
- operations ["post" ] = self ._get_operations_for_all (tag_name , default_parameters )
376
- operations ["post" ]["responses" ] = {
377
- "201" : {"description" : "Created" },
378
- "202" : {"description" : "Accepted" },
379
- "403" : {"description" : "This implementation does not accept client-generated IDs" },
380
- "404" : {"description" : "Not Found" },
381
- "409" : {"description" : "Conflict" },
382
- }
383
- operations ["post" ]["parameters" ].append (
384
- {
385
- "name" : "POST body" ,
386
- "in" : "body" ,
387
- "schema" : schema ,
388
- "required" : True ,
389
- "description" : f"{ tag_name } attributes" ,
390
- }
391
- )
426
+ operations ["post" ] = self ._get_operations_for_post (schema , tag_name , default_parameters )
392
427
if "patch" in methods :
393
- operations ["patch" ] = self ._get_operations_for_all (tag_name , default_parameters )
394
- operations ["patch" ]["responses" ] = {
395
- "200" : {"description" : "Success" },
396
- "201" : {"description" : "Created" },
397
- "204" : {"description" : "No Content" },
398
- "403" : {"description" : "Forbidden" },
399
- "404" : {"description" : "Not Found" },
400
- "409" : {"description" : "Conflict" },
401
- }
402
- operations ["patch" ]["parameters" ].append (self .param_id )
403
- operations ["patch" ]["parameters" ].append (
404
- {
405
- "name" : "POST body" ,
406
- "in" : "body" ,
407
- "schema" : schema ,
408
- "required" : True ,
409
- "description" : f"{ tag_name } attributes" ,
410
- }
411
- )
428
+ operations ["patch" ] = self ._get_operations_for_patch (schema , tag_name , default_parameters )
412
429
if "delete" in methods :
413
- operations ["delete" ] = self ._get_operations_for_all (tag_name , default_parameters )
414
- operations ["delete" ]["parameters" ].append (self .param_id )
415
- operations ["delete" ]["responses" ] = {
416
- "200" : {"description" : "Success" },
417
- "202" : {"description" : "Accepted" },
418
- "204" : {"description" : "No Content" },
419
- "403" : {"description" : "Forbidden" },
420
- "404" : {"description" : "Not Found" },
421
- }
430
+ operations ["delete" ] = self ._get_operations_for_delete (tag_name , default_parameters )
422
431
rule = None
423
432
for i_rule in self .app .url_map ._rules :
424
433
if i_rule .rule == path :
0 commit comments