Browse Source

Better default schema name

Guénaël Muller 6 years ago
parent
commit
576ed63c4d
1 changed files with 21 additions and 2 deletions
  1. 21 2
      hapic/doc.py

+ 21 - 2
hapic/doc.py View File

@@ -157,6 +157,7 @@ class DocGenerator(object):
157 157
                 'apispec.ext.marshmallow',
158 158
             ),
159 159
             auto_referencing=True,
160
+            schema_name_resolver=generate_schema_name
160 161
         )
161 162
 
162 163
         schemas = []
@@ -261,5 +262,23 @@ class DocGenerator(object):
261 262
 
262 263
 
263 264
 # TODO BS 20171109: Must take care of already existing definition names
264
-def generate_schema_name(schema):
265
-    return schema.__name__
265
+def generate_schema_name(schema: marshmallow.Schema):
266
+    """
267
+    Return best candidate name for one schema cls or instance.
268
+    :param schema: instance or cls schema
269
+    :return: best schema name
270
+    """
271
+    if not isinstance(schema, type):
272
+        schema = type(schema)
273
+
274
+    if getattr(schema, '_schema_name', None):
275
+        if schema.opts.exclude:
276
+            schema_name = "{}_without".format(schema.__name__)
277
+            for elem in sorted(schema.opts.exclude):
278
+                schema_name="{}_{}".format(schema_name, elem)
279
+        else:
280
+            schema_name = schema._schema_name
281
+    else:
282
+        schema_name = schema.__name__
283
+
284
+    return schema_name