Parcourir la source

add many support for input/output body

Guénaël Muller il y a 6 ans
Parent
révision
6113b2c5de
1 fichiers modifiés avec 26 ajouts et 20 suppressions
  1. 26 20
      hapic/doc.py

+ 26 - 20
hapic/doc.py Voir le fichier

14
 from hapic.description import ControllerDescription
14
 from hapic.description import ControllerDescription
15
 
15
 
16
 
16
 
17
+def generate_schema_ref(spec, schema):
18
+    schema_class = spec.schema_class_resolver(
19
+        spec,
20
+        schema
21
+    )
22
+    ref = {
23
+        '$ref': '#/definitions/{}'.format(
24
+            spec.schema_name_resolver(schema_class)
25
+        )
26
+    }
27
+    if schema.many:
28
+        ref = {
29
+            'type': 'array',
30
+            'items': ref
31
+        }
32
+    return ref
33
+
34
+
17
 def bottle_generate_operations(
35
 def bottle_generate_operations(
18
     spec,
36
     spec,
19
     route: RouteRepresentation,
37
     route: RouteRepresentation,
20
     description: ControllerDescription,
38
     description: ControllerDescription,
21
 ):
39
 ):
22
     method_operations = dict()
40
     method_operations = dict()
23
-    # TODO : Convert many=True in list of elems
24
-    # schema based
25
     if description.input_body:
41
     if description.input_body:
26
-        schema_class = spec.schema_class_resolver(
27
-            spec,
28
-            description.input_body.wrapper.processor.schema
29
-        )
30
         method_operations.setdefault('parameters', []).append({
42
         method_operations.setdefault('parameters', []).append({
31
             'in': 'body',
43
             'in': 'body',
32
             'name': 'body',
44
             'name': 'body',
33
-            'schema': {
34
-                '$ref': '#/definitions/{}'.format(
35
-                    spec.schema_name_resolver(schema_class)
36
-                )
37
-            }
45
+            'schema': generate_schema_ref(
46
+                spec,
47
+                description.input_body.wrapper.processor.schema,
48
+            )
38
         })
49
         })
39
 
50
 
40
     if description.output_body:
51
     if description.output_body:
41
-        schema_class = spec.schema_class_resolver(
42
-            spec,
43
-            description.output_body.wrapper.processor.schema
44
-        )
45
         method_operations.setdefault('responses', {})\
52
         method_operations.setdefault('responses', {})\
46
             [int(description.output_body.wrapper.default_http_code)] = {
53
             [int(description.output_body.wrapper.default_http_code)] = {
47
                 'description': str(int(description.output_body.wrapper.default_http_code)),  # nopep8
54
                 'description': str(int(description.output_body.wrapper.default_http_code)),  # nopep8
48
-                'schema': {
49
-                    '$ref': '#/definitions/{}'.format(
50
-                        spec.schema_name_resolver(schema_class)
51
-                    )
52
-                }
55
+                'schema': generate_schema_ref(
56
+                    spec,
57
+                    description.output_body.wrapper.processor.schema,
58
+                )
53
             }
59
             }
54
 
60
 
55
     if description.output_file:
61
     if description.output_file: