Skip to content

Commit

Permalink
Merge 7daa074 into 3806981
Browse files Browse the repository at this point in the history
  • Loading branch information
MingboPeng committed Jan 23, 2020
2 parents 3806981 + 7daa074 commit bdd5112
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -9,3 +9,5 @@ test.db
test.py
out.txt
docs/*.json
.eggs
.pytest_cache
20 changes: 20 additions & 0 deletions honeybee_schema/_openapi.py
Expand Up @@ -81,6 +81,25 @@ def get_openapi(
}
tags.append(tag)

# sort properties order: put required parameters at begining of the list
s = schemas[name]
if not 'required' in s:
continue
properties = s['properties']
required = s['required']

sorted_props = {}
optional = {}
for prop, value in properties.items():
if prop in required:
sorted_props[prop] = value
else:
optional[prop] = value

sorted_props.update(optional)

s['properties'] = sorted_props

tag_names.sort()
open_api['tags'] = tags
open_api['x-tagGroups'][0]['tags'] = tag_names
Expand All @@ -90,5 +109,6 @@ def get_openapi(
return open_api



if __name__ == '__main__':
get_openapi()
34 changes: 34 additions & 0 deletions tests/test_sortProps.py
@@ -0,0 +1,34 @@

from honeybee_schema.model import Model
from pydantic.schema import schema
import pytest

def test_sort_required_params():
definitions = schema([Model], ref_prefix='#/components/schemas/')

schemas = definitions['definitions']
schema_names = list(schemas.keys())
schema_names.sort()

for name in schema_names:
s = schemas[name]
if not 'required' in s:
continue
properties = s['properties']
required = s['required']

sorted_props = {}
optional = {}
for prop, value in properties.items():
if prop in required:
sorted_props[prop] = value
else:
optional[prop] = value

sorted_props.update(optional)

if not list(sorted_props.keys())[0:len(required)] == required:
pytest.fail("Class %s: in this object, not all required parameters are at top of the list"%name)



0 comments on commit bdd5112

Please sign in to comment.