Skip to content

Commit

Permalink
Merge pull request #58 from guokr/fix_issue_42
Browse files Browse the repository at this point in the history
Fixes issue #42
  • Loading branch information
rejown committed Mar 25, 2016
2 parents fd2e621 + 14d0507 commit 0cdeb29
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 5 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Command Options:

## Examples:

Generate example-app from [apis.yml](https://github.com/guokr/swagger-py-codegen/blob/master/api.yml "Title"):
Generate example-app from [apis.yml](https://github.com/guokr/swagger-py-codegen/blob/master/api.yml "Title"):

$tree
.
Expand Down Expand Up @@ -72,9 +72,11 @@ Start example-app:
$ cd demo
$ python __init__.py

And generate example-app-ui from apis.yml with ui:
And generate example-app-ui from apis.yml with ui:

$ swagger_py_codegen -s api.yml example-app-ui -p demo-ui --ui
$ swagger_py_codegen -s api.yml example-app-ui -p demo-ui --ui --spec

Then you can visit http://127.0.0.1:5000/static/swagger-ui/index.html in a browser.

## Authors
--------
Expand Down
2 changes: 1 addition & 1 deletion swagger_py_codegen/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .command import generate

__version__ = '0.1.13'
__version__ = '0.1.14'
9 changes: 8 additions & 1 deletion swagger_py_codegen/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ def has(self, key):

def _normalize_dict(schema, data):
result = {}
data = DataWrapper(data)
if not isinstance(data, DataWrapper):
data = DataWrapper(data)
for key, _schema in schema.get('properties', {}).iteritems():
# set default
type_ = _schema.get('type', 'object')
Expand All @@ -167,6 +168,12 @@ def _normalize_dict(schema, data):
elif key in schema.get('required', []):
errors.append(dict(name='property_missing',
message='`%s` is required' % key))

for _schema in schema.get('allOf', []):
rs_component = _normalize(_schema, data)
rs_component.update(result)
result = rs_component

return result

def _normalize_list(schema, data):
Expand Down
120 changes: 120 additions & 0 deletions tests/test_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,123 @@ def test_normalize_04():
result, errors = normalize(schema, default)
assert errors
assert len(errors) == 2


def test_normalize_05():
from swagger_py_codegen.jsonschema import normalize
class Pet(object):
def __init__(self, name, petType):
self.name = name
self.petType = petType

class Cat(Pet):
def __init__(self, huntingSkill, **kwargs):
if kwargs:
super(Cat, self).__init__(**kwargs)
self.huntingSkill = huntingSkill

schema = {
'description': 'A representation of a cat',
'allOf': [
{
'discriminator': 'petType',
'required': ['name', 'petType'],
'type': 'object',
'properties': {
'petType': {'type': 'string'},
'name': {'type': 'string'}
}
},
{
'required': ['huntingSkill'],
'type': 'object',
'properties': {
'huntingSkill': {
'default': 'lazy',
'enum': ['clueless', 'lazy', 'adventurous', 'aggressive'],
'type': 'string',
'description': 'The measured skill for hunting'
}
}
}
]
}

result, errors = normalize(schema, Cat(huntingSkill='lazy', name='bob', petType='cat'))
assert errors == []
assert result['name'] == 'bob'
assert result['petType'] == 'cat'
assert result['huntingSkill'] == 'lazy'

result, errors = normalize(schema, Cat(huntingSkill='lazy'))
assert result['huntingSkill'] == 'lazy'
assert errors
assert len(errors) == 2


def test_normalize_06():
from swagger_py_codegen.jsonschema import normalize

schema = {
'description': 'A representation of a cat',
'allOf': [
{
'discriminator': 'petType',
'required': ['name', 'petType'],
'type': 'object',
'properties': {
'petType': {'type': 'string'},
'name': {'type': 'string'}
}
},
{
'required': ['huntingSkill'],
'type': 'object',
'properties': {
'huntingSkill': {
'default': 'lazy',
'enum': ['clueless', 'lazy', 'adventurous', 'aggressive'],
'type': 'string',
'description': 'The measured skill for hunting'
}
}
}
]
}
default = {
'name': 'bob',
'petType': 'cat',
'huntingSkill': 'lazy'
}
result, errors = normalize(schema, default)
assert errors == []
assert result['name'] == 'bob'
assert result['petType'] == 'cat'
assert result['huntingSkill'] == 'lazy'

default = {
'name': 'bob',
'petType': 'cat',
}
result, errors = normalize(schema, default)
assert errors == []
assert result['name'] == 'bob'
assert result['petType'] == 'cat'
assert result['huntingSkill'] == 'lazy'

default = {
'name': 'bob',
'huntingSkill': 'lazy'
}
result, errors = normalize(schema, default)
assert errors
assert len(errors) == 1
assert result['huntingSkill'] == 'lazy'

default = {
'huntingSkill': 'lazy'
}
result, errors = normalize(schema, default)
assert errors
assert len(errors) == 2
assert result['huntingSkill'] == 'lazy'

0 comments on commit 0cdeb29

Please sign in to comment.