Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 2d25008

Browse files
committed
Handle nested documetns
1 parent a118bac commit 2d25008

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

openapi_codec/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from openapi_codec.decode import _parse_document
99

1010

11-
__version__ = '1.1.1'
11+
__version__ = '1.1.2'
1212

1313

1414
class OpenAPICodec(BaseCodec):

openapi_codec/encode.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import coreapi
21
from coreapi.compat import urlparse
3-
from openapi_codec.utils import get_method, get_encoding, get_location
2+
from openapi_codec.utils import get_method, get_encoding, get_location, get_links_from_document
43

54

65
def generate_swagger_object(document):
@@ -35,16 +34,18 @@ def _add_tag_prefix(item):
3534

3635
def _get_links(document):
3736
"""
38-
Return a list of (operation_id, [tags], link)
37+
Return a list of (operation_id, link, [tags])
3938
"""
4039
# Extract all the links from the first or second level of the document.
4140
links = []
42-
for key, link in document.links.items():
43-
links.append((key, link, []))
44-
for key0, obj in document.data.items():
45-
if isinstance(obj, coreapi.Object):
46-
for key1, link in obj.links.items():
47-
links.append((key1, link, [key0]))
41+
for keys, link in get_links_from_document(document):
42+
if len(keys) > 1:
43+
operation_id = '_'.join(keys[1:])
44+
tags = [keys[0]]
45+
else:
46+
operation_id = keys[0]
47+
tags = []
48+
links.append((operation_id, link, tags))
4849

4950
# Determine if the operation ids each have unique names or not.
5051
operation_ids = [item[0] for item in links]

openapi_codec/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
def get_links_from_document(node, keys=()):
2+
links = []
3+
for key, link in getattr(node, 'links', {}).items():
4+
# Get all the resources at this level
5+
index = keys + (key,)
6+
links.append((index, link))
7+
for key, child in getattr(node, 'data', {}).items():
8+
# Descend into any nested structures.
9+
index = keys + (key,)
10+
links.extend(get_links_from_document(child, index))
11+
return links
12+
13+
114
def get_method(link):
215
method = link.action.lower()
316
if not method:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy