Guénaël Muller 6 years ago
parent
commit
95ea8916b7
1 changed files with 25 additions and 20 deletions
  1. 25 20
      hapic/doc.py

+ 25 - 20
hapic/doc.py View File

@@ -17,25 +17,30 @@ def bottle_generate_operations(
17 17
     description: ControllerDescription,
18 18
 ):
19 19
     method_operations = dict()
20
-
20
+    # TODO : Convert many=True in list of elems
21 21
     # schema based
22 22
     if description.input_body:
23
-        schema_class = type(description.input_body.wrapper.processor.schema)
23
+        schema_class = spec.schema_class_resolver(
24
+            description.input_body.wrapper.processor.schema)
24 25
         method_operations.setdefault('parameters', []).append({
25 26
             'in': 'body',
26 27
             'name': 'body',
27 28
             'schema': {
28
-                '$ref': '#/definitions/{}'.format(schema_class.__name__)
29
+                '$ref': '#/definitions/{}'.format(
30
+                    spec.schema_name_resolver(schema_class))
29 31
             }
30 32
         })
31 33
 
32 34
     if description.output_body:
33
-        schema_class = type(description.output_body.wrapper.processor.schema)
35
+        schema_class = spec.schema_class_resolver(
36
+            description.output_body.wrapper.processor.schema)
34 37
         method_operations.setdefault('responses', {})\
35 38
             [int(description.output_body.wrapper.default_http_code)] = {
36 39
                 'description': str(description.output_body.wrapper.default_http_code),  # nopep8
37 40
                 'schema': {
38
-                    '$ref': '#/definitions/{}'.format(schema_class.__name__)
41
+                    '$ref': '#/definitions/{}'.format(
42
+                        spec.schema_name_resolver(schema_class)
43
+                    )
39 44
                 }
40 45
             }
41 46
 
@@ -50,18 +55,21 @@ def bottle_generate_operations(
50 55
 
51 56
     if description.errors:
52 57
         for error in description.errors:
53
-            schema_class = type(error.wrapper.schema)
58
+            schema_class = spec.schema_class_resolver(error.wrapper.schema)
54 59
             method_operations.setdefault('responses', {})\
55 60
                 [int(error.wrapper.http_code)] = {
56 61
                     'description': str(error.wrapper.http_code),
57 62
                     'schema': {
58
-                        '$ref': '#/definitions/{}'.format(schema_class.__name__)  # nopep8
63
+                        '$ref': '#/definitions/{}'.format(
64
+                            spec.schema_name_resolver(schema_class)
65
+                        )
59 66
                     }
60 67
                 }
61 68
 
62 69
     # jsonschema based
63 70
     if description.input_path:
64
-        schema_class = type(description.input_path.wrapper.processor.schema)
71
+        schema_class = spec.schema_class_resolver(
72
+            description.input_path.wrapper.processor.schema)
65 73
         # TODO: look schema2parameters ?
66 74
         jsonschema = schema2jsonschema(schema_class, spec=spec)
67 75
         for name, schema in jsonschema.get('properties', {}).items():
@@ -73,7 +81,8 @@ def bottle_generate_operations(
73 81
             })
74 82
 
75 83
     if description.input_query:
76
-        schema_class = type(description.input_query.wrapper.processor.schema)
84
+        schema_class = spec.schema_class_resolver(
85
+            description.input_query.wrapper.processor.schema)
77 86
         jsonschema = schema2jsonschema(schema_class, spec=spec)
78 87
         for name, schema in jsonschema.get('properties', {}).items():
79 88
             method_operations.setdefault('parameters', []).append({
@@ -115,7 +124,7 @@ class DocGenerator(object):
115 124
             plugins=(
116 125
                 'apispec.ext.marshmallow',
117 126
             ),
118
-            schema_name_resolver=generate_schema_name,
127
+            auto_referencing=True,
119 128
         )
120 129
 
121 130
         schemas = []
@@ -124,26 +133,27 @@ class DocGenerator(object):
124 133
             description = controller.description
125 134
 
126 135
             if description.input_body:
127
-                schemas.append(type(
136
+                schemas.append(spec.schema_class_resolver(
128 137
                     description.input_body.wrapper.processor.schema
129 138
                 ))
130 139
 
131 140
             if description.input_forms:
132
-                schemas.append(type(
141
+                schemas.append(spec.schema_class_resolver(
133 142
                     description.input_forms.wrapper.processor.schema
134 143
                 ))
135 144
 
136 145
             if description.output_body:
137
-                schemas.append(type(
146
+                schemas.append(spec.schema_class_resolver(
138 147
                     description.output_body.wrapper.processor.schema
139 148
                 ))
140 149
 
141 150
             if description.errors:
142 151
                 for error in description.errors:
143
-                    schemas.append(type(error.wrapper.schema))
152
+                    schemas.append(
153
+                        spec.schema_class_resolver(error.wrapper.schema))
144 154
 
145 155
         for schema in set(schemas):
146
-            spec.definition(schema.__name__, schema=schema)
156
+            spec.definition(spec.schema_name_resolver(schema), schema=schema)
147 157
 
148 158
         # add views
149 159
         # with app.test_request_context():
@@ -174,8 +184,3 @@ class DocGenerator(object):
174 184
             spec.add_path(path)
175 185
 
176 186
         return spec.to_dict()
177
-
178
-
179
-# TODO BS 20171109: Must take care of already existing definition names
180
-def generate_schema_name(schema):
181
-    return schema.__name__