add model attribute name json key mapping in python clent

This commit is contained in:
geekerzp 2015-03-24 11:55:20 +08:00
parent fe82d7dc05
commit 07237f8eb9
2 changed files with 20 additions and 6 deletions

View File

@ -17,12 +17,17 @@ Copyright 2015 Reverb Technologies, Inc.
{{#models}}
{{#model}}
class {{classname}}:
class {{classname}}(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually."""
def __init__(self):
"""
Attributes:
swaggerTypes (dict): The key is attribute name and the value is attribute type.
attributeMap (dict): The key is attribute name and the value is json key in definition.
"""
self.swaggerTypes = {
{{#vars}}
'{{name}}': '{{{datatype}}}'{{#hasMore}},
@ -30,6 +35,11 @@ class {{classname}}:
{{/vars}}{{newline}}
}
self.attributeMap = {
{{#vars}}
'{{name}}': '{{baseName}}'{{#hasMore}},{{/hasMore}}
{{/vars}}
}
{{#vars}}
{{#description}}#{{description}}

View File

@ -131,10 +131,14 @@ class ApiClient(object):
if isinstance(obj, dict):
objDict = obj
else:
objDict = obj.__dict__
# Convert model obj to dict except attributes `swaggerTypes`, `attributeMap`
# and attributes which value is not None.
# Convert attribute name to json key in model definition for request.
objDict = {obj.attributeMap[key]: val
for key, val in obj.__dict__.iteritems()
if key != 'swaggerTypes' and key != 'attributeMap' and val is not None}
return {key: ApiClient.sanitizeForSerialization(val)
for (key, val) in objDict.iteritems()
if key != 'swaggerTypes'}
for (key, val) in objDict.iteritems()}
def buildMultipartFormData(self, postData, files):
def escape_quotes(s):
@ -199,8 +203,8 @@ class ApiClient(object):
instance = objClass()
for attr, attrType in instance.swaggerTypes.iteritems():
if obj is not None and attr in obj and type(obj) in [list, dict]:
value = obj[attr]
if obj is not None and instance.attributeMap[attr] in obj and type(obj) in [list, dict]:
value = obj[instance.attributeMap[attr]]
if attrType in ['str', 'int', 'long', 'float', 'bool']:
attrType = eval(attrType)
try: