8000 Ensure operationId is unique · Pix4D/python-openapi-codec@d74d456 · GitHub
[go: up one dir, main page]

Skip to content

Commit d74d456

Browse files
committed
Ensure operationId is unique
1 parent 0fdd8c0 commit d74d456

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

openapi_codec/decode.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ def _parse_document(data, base_url=None):
6868
tags = get_strings(_get_list(operation, 'tags'))
6969
operation_id = _get_string(operation, 'operationId')
7070
if tags:
71-
for tag in tags:
72-
if tag not in content:
73-
content[tag] = {}
74-
content[tag][operation_id] = link
71+
tag = tags[0]
72+
prefix = tag + '_'
73+
if operation_id.startswith(prefix):
74+
operation_id = operation_id[len(prefix):]
75+
if tag not in content:
76+
content[tag] = {}
77+
content[tag][operation_id] = link
7578
else:
7679
content[operation_id] = link
7780

openapi_codec/encode.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import coreapi
12
from coreapi.compat import urlparse
23
from 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+
2355
def _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

Comments
 (0)
0