1+ import coreapi
12from coreapi .compat import urlparse
23from openapi_codec .utils import get_method , get_encoding , get_location
34
@@ -20,35 +21,54 @@ def generate_swagger_object(document):
2021 }
2122
2223
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+
2355def _get_paths_object (document ):
2456 paths = {}
2557
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 :
2861 if link .url not in paths :
2962 paths [link .url ] = {}
3063
3164 method = get_method (link )
32- operation = _get_operation (link , operation_id )
65+ operation = _get_operation (operation_id , link , tags )
3366 paths [link .url ].update ({method : operation })
3467
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-
4868 return paths
4969
5070
51- def _get_operation (link , operation_id , tags = None ):
71+ def _get_operation (operation_id , link , tags ):
5272 encoding = get_encoding (link )
5373
5474 operation = {
0 commit comments