Skip to content

Commit

Permalink
Merge pull request #60 from guokr/add-feature-additionalProperties
Browse files Browse the repository at this point in the history
Add feature additional properties
  • Loading branch information
softlns committed Mar 30, 2016
2 parents 0cdeb29 + 174ff8b commit 3fd981a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
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.14'
__version__ = '0.1.15'
12 changes: 12 additions & 0 deletions swagger_py_codegen/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,16 @@ def has(self, key):
return key in self.data
return hasattr(self.data, key)

def keys(self):
if isinstance(self.data, dict):
return self.data.keys()
return vars(self.data).keys()

def _normalize_dict(schema, data):
result = {}
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 @@ -174,6 +180,12 @@ def _normalize_dict(schema, data):
rs_component.update(result)
result = rs_component

additional_properties_schema = schema.get('additionalProperties', False)
if additional_properties_schema:
aproperties_set = set(data.keys()) - set(result.keys())
for pro in aproperties_set:
result[pro] = _normalize(additional_properties_schema, data.get(pro))

return result

def _normalize_list(schema, data):
Expand Down
102 changes: 102 additions & 0 deletions tests/test_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,3 +731,105 @@ def test_normalize_06():
assert errors
assert len(errors) == 2
assert result['huntingSkill'] == 'lazy'


def test_normalize_07():
from swagger_py_codegen.jsonschema import normalize

class A(object):
def __init__(self, visible_property):
self.visible_property = visible_property

schema = {
'additionalProperties': {'type': 'string'},
'discriminator': 'response info',
'type': 'object',
'properties': {
'visible_property': {
'type': 'string',
'description': 'This is a property that you can see'
}
}
}

data = A(visible_property='the property you see')
data.additional_property1 = 'test1'
data.additional_property2 = 'test2'
result, errors = normalize(schema, data)
assert errors == []
assert result['visible_property'] == 'the property you see'
assert result['additional_property1'] == 'test1'
assert result['additional_property2'] == 'test2'

default = {
'visible_property': 'default visible property',
'additional_property01': 'test01',
'additional_property02': 'test02',
}
result, errors = normalize(schema, default)
assert errors == []
assert result['visible_property'] == 'default visible property'
assert result['additional_property01'] == 'test01'
assert result['additional_property02'] == 'test02'


def test_normalize_08():
from swagger_py_codegen.jsonschema import normalize

class A(object):
def __init__(self, visible_property):
self.visible_property = visible_property

class B(object):
def __init__(self, subobject):
self.subobject = subobject

schema = {
'additionalProperties': {
'required': ['subobject'],
'properties': {
'subobject': {
'type': 'string',
'description': 'Some string value'
}
}
},
'discriminator': 'response info',
'type': 'object',
'properties': {
'visible_property': {
'type': 'string',
'description': 'This is a property that you can see'
}
}
}

data = A(visible_property='default property')
data.additional_property1 = B('test1')
data.additional_property2 = B('test2')
result, errors = normalize(schema, data)
assert errors == []
assert result['visible_property'] == 'default property'
assert result['additional_property1'] == {'subobject': 'test1'}
assert result['additional_property2'] == {'subobject': 'test2'}

default = {
'visible_property': 'default visible property',
'additional_property01': {'subobject': 'test01'},
'additional_property02': {},
}
result, errors = normalize(schema, default)
assert len(errors) == 1
assert result['visible_property'] == 'default visible property'
assert result['additional_property01'] == {'subobject': 'test01'}

default = {
'visible_property': 'default visible property',
'additional_property01': {'subobject': 'test01'},
'additional_property02': {'subobject': 'test02'},
}
result, errors = normalize(schema, default)
assert errors == []
assert result['visible_property'] == 'default visible property'
assert result['additional_property01'] == {'subobject': 'test01'}
assert result['additional_property02'] == {'subobject': 'test02'}

0 comments on commit 3fd981a

Please sign in to comment.