1
+ import coreapi
1
2
from coreapi .compat import urlparse
2
3
from openapi_codec .utils import get_method , get_encoding , get_location
3
4
@@ -20,35 +21,54 @@ def generate_swagger_object(document):
20
21
}
21
22
22
23
24
+ def _add_tag_prefix (item ):
25
+ operation_id , link , tags = item
26
+ if tags :
27
+ operation_id = tags [0 ] + '_' + operation_id
28
+ return (operation_id , link , tags )
29
+
30
+
31
+ def _get_links (document ):
32
+ """
33
+ Return a list of (operation_id, [tags], link)
34
+ """
35
+ # Extract all the links from the first or second level of the document.
36
+ links = []
37
+ for key , link in document .links .items ():
38
+ links .append ((key , link , []))
39
+ for key0 , obj in document .data .items ():
40
+ if isinstance (obj , coreapi .Object ):
41
+ for key1 , link in obj .links .items ():
42
+ links .append ((key1 , link , [key0 ]))
43
+
44
+ # Determine if the operation ids each have unique names or not.
45
+ operation_ids = [item [0 ] for item in links ]
46
+ unique = len (set (operation_ids )) == len (links )
47
+
48
+ # If the operation ids are not unique, then prefix them with the tag.
49
+ if not unique :
50
+ return [_add_tag_prefix (item ) for item in links ]
51
+
52
+ return links
53
+
54
+
23
55
def _get_paths_object (document ):
24
56
paths = {}
25
57
26
- # Top-level links. We do not include a swagger 'tag' for these.
27
- for operation_id , link in document .links .items ():
58
+ links = _get_links (document )
59
+
60
+ for operation_id , link , tags in links :
28
61
if link .url not in paths :
29
62
paths [link .url ] = {}
30
63
31
64
method = get_method (link )
32
- operation = _get_operation (link , operation_id )
65
+ operation = _get_operation (operation_id , link , tags )
33
66
paths [link .url ].update ({method : operation })
34
67
35
- # Second-level links. We include a swagger 'tag' for these.
36
- for tag , object_ in document .data .items ():
37
- if not hasattr (object_ , 'links' ):
38
- continue
39
-
40
- for operation_id , link in object_ .links .items ():
41
- if link .url not in paths :
42
- paths [link .url ] = {}
43
-
44
- method = get_method (link )
45
- operation = _get_operation (link , operation_id , tags = [tag ])
46
- paths [link .url ].update ({method : operation })
47
-
48
68
return paths
49
69
50
70
51
- def _get_operation (link , operation_id , tags = None ):
71
+ def _get_operation (operation_id , link , tags ):
52
72
encoding = get_encoding (link )
53
73
54
74
operation = {
0 commit comments