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

Commit b9621a9

Browse files
committed
Use ordered output for Swagger specs
1 parent 401a407 commit b9621a9

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
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.2'
11+
__version__ = '1.1.3'
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,3 +1,4 @@
1+
from collections import OrderedDict
12
from coreapi.compat import urlparse
23
from openapi_codec.utils import get_method, get_encoding, get_location, get_links_from_document
34

@@ -8,20 +9,20 @@ def generate_swagger_object(document):
89
"""
910
parsed_url = urlparse.urlparse(document.url)
1011

11-
swagger = {
12-
'swagger': '2.0',
13-
'info': {
14-
'title': document.title,
15-
'version': '' # Required by the spec
16-
},
17-
'paths': _get_paths_object(document)
18-
}
12+
swagger = OrderedDict()
13+
14+
swagger['swagger'] = '2.0'
15+
swagger['info'] = OrderedDict()
16+
swagger['info']['title'] = document.title
17+
swagger['info']['version'] = '' # Required by the spec
1918

2019
if parsed_url.netloc:
2120
swagger['host'] = parsed_url.netloc
2221
if parsed_url.scheme:
2322
swagger['schemes'] = [parsed_url.scheme]
2423

24+
swagger['paths'] = _get_paths_object(document)
25+
2526
return swagger
2627

2728

@@ -59,7 +60,7 @@ def _get_links(document):
5960

6061

6162
def _get_paths_object(document):
62-
paths = {}
63+
paths = OrderedDict()
6364

6465
links = _get_links(document)
6566

openapi_codec/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
def link_sorting_key(link_item):
2+
keys, link = link_item
3+
action_priority = {
4+
'': 0, 'get': 0,
5+
'post': 1,
6+
'put': 2,
7+
'patch': 3,
8+
'delete': 4
9+
}.get(link.action, 5)
10+
return (link.url, action_priority)
11+
12+
113
def get_links_from_document(node, keys=()):
214
links = []
315
for key, link in getattr(node, 'links', {}).items():
@@ -8,7 +20,7 @@ def get_links_from_document(node, keys=()):
820
# Descend into any nested structures.
921
index = keys + (key,)
1022
links.extend(get_links_from_document(child, index))
11-
return links
23+
return sorted(links, key=link_sorting_key)
1224

1325

1426
def get_method(link):

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