From 492ab20c00cd50ec1b6d3436e07f9a97dce042c8 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Sat, 28 Mar 2015 10:42:02 -0700 Subject: [PATCH] fix for issue 567, added reserved words, rebuilt client --- .../petstore/python/client/models/Category.py | 14 ++- .../petstore/python/client/models/Order.py | 22 ++++- .../petstore/python/client/models/Pet.py | 22 ++++- .../petstore/python/client/models/Tag.py | 14 ++- .../petstore/python/client/models/User.py | 26 +++++- .../client/petstore/python/client/swagger.py | 86 +++++++++++-------- 6 files changed, 142 insertions(+), 42 deletions(-) diff --git a/samples/client/petstore/python/client/models/Category.py b/samples/client/petstore/python/client/models/Category.py index 245397c3f1b..3d64348d1d0 100644 --- a/samples/client/petstore/python/client/models/Category.py +++ b/samples/client/petstore/python/client/models/Category.py @@ -15,12 +15,17 @@ Copyright 2015 Reverb Technologies, Inc. limitations under the License. """ -class Category: +class Category(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 = { 'id': 'long', @@ -30,6 +35,13 @@ class Category: } + self.attributeMap = { + + 'id': 'id', + + 'name': 'name' + + } diff --git a/samples/client/petstore/python/client/models/Order.py b/samples/client/petstore/python/client/models/Order.py index 60bbc0f9913..244fa872cbd 100644 --- a/samples/client/petstore/python/client/models/Order.py +++ b/samples/client/petstore/python/client/models/Order.py @@ -15,12 +15,17 @@ Copyright 2015 Reverb Technologies, Inc. limitations under the License. """ -class Order: +class Order(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 = { 'id': 'long', @@ -42,6 +47,21 @@ class Order: } + self.attributeMap = { + + 'id': 'id', + + 'petId': 'petId', + + 'quantity': 'quantity', + + 'shipDate': 'shipDate', + + 'status': 'status', + + 'complete': 'complete' + + } diff --git a/samples/client/petstore/python/client/models/Pet.py b/samples/client/petstore/python/client/models/Pet.py index c4f024b996c..4c4ee116c09 100644 --- a/samples/client/petstore/python/client/models/Pet.py +++ b/samples/client/petstore/python/client/models/Pet.py @@ -15,12 +15,17 @@ Copyright 2015 Reverb Technologies, Inc. limitations under the License. """ -class Pet: +class Pet(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 = { 'id': 'long', @@ -42,6 +47,21 @@ class Pet: } + self.attributeMap = { + + 'id': 'id', + + 'category': 'category', + + 'name': 'name', + + 'photoUrls': 'photoUrls', + + 'tags': 'tags', + + 'status': 'status' + + } diff --git a/samples/client/petstore/python/client/models/Tag.py b/samples/client/petstore/python/client/models/Tag.py index bd565e2bab3..5d4411799e6 100644 --- a/samples/client/petstore/python/client/models/Tag.py +++ b/samples/client/petstore/python/client/models/Tag.py @@ -15,12 +15,17 @@ Copyright 2015 Reverb Technologies, Inc. limitations under the License. """ -class Tag: +class Tag(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 = { 'id': 'long', @@ -30,6 +35,13 @@ class Tag: } + self.attributeMap = { + + 'id': 'id', + + 'name': 'name' + + } diff --git a/samples/client/petstore/python/client/models/User.py b/samples/client/petstore/python/client/models/User.py index 81466c88530..f735aed0082 100644 --- a/samples/client/petstore/python/client/models/User.py +++ b/samples/client/petstore/python/client/models/User.py @@ -15,12 +15,17 @@ Copyright 2015 Reverb Technologies, Inc. limitations under the License. """ -class User: +class User(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 = { 'id': 'long', @@ -48,6 +53,25 @@ class User: } + self.attributeMap = { + + 'id': 'id', + + 'username': 'username', + + 'firstName': 'firstName', + + 'lastName': 'lastName', + + 'email': 'email', + + 'password': 'password', + + 'phone': 'phone', + + 'userStatus': 'userStatus' + + } diff --git a/samples/client/petstore/python/client/swagger.py b/samples/client/petstore/python/client/swagger.py index 8d9053025d1..1cfc60981be 100644 --- a/samples/client/petstore/python/client/swagger.py +++ b/samples/client/petstore/python/client/swagger.py @@ -19,7 +19,7 @@ import string from models import * -class ApiClient: +class ApiClient(object): """Generic API client for Swagger client library builds Attributes: @@ -41,13 +41,13 @@ class ApiClient: headers = {} if headerParams: for param, value in headerParams.iteritems(): - headers[param] = value + headers[param] = ApiClient.sanitizeForSerialization(value) if self.headerName: - headers[self.headerName] = self.headerValue + headers[self.headerName] = ApiClient.sanitizeForSerialization(self.headerValue) if self.cookie: - headers['Cookie'] = self.cookie + headers['Cookie'] = ApiClient.sanitizeForSerialization(self.cookie) data = None @@ -55,8 +55,8 @@ class ApiClient: # Need to remove None values, these should not be sent sentQueryParams = {} for param, value in queryParams.items(): - if value != None: - sentQueryParams[param] = value + if value is not None: + sentQueryParams[param] = ApiClient.sanitizeForSerialization(value) url = url + '?' + urllib.urlencode(sentQueryParams) if method in ['GET']: @@ -65,7 +65,7 @@ class ApiClient: elif method in ['POST', 'PUT', 'DELETE']: if postData: - postData = self.sanitizeForSerialization(postData) + postData = ApiClient.sanitizeForSerialization(postData) if 'Content-type' not in headers: headers['Content-type'] = 'application/json' data = json.dumps(postData) @@ -107,34 +107,38 @@ class ApiClient: else: return urllib.quote(str(obj)) - def sanitizeForSerialization(self, obj): - """Dump an object into JSON for POSTing.""" + @staticmethod + def sanitizeForSerialization(obj): + """ + Sanitize an object for Request. - if type(obj) == type(None): + If obj is None, return None. + If obj is str, int, long, float, bool, return directly. + If obj is datetime.datetime, datetime.date convert to string in iso8601 format. + If obj is list, santize each element in the list. + If obj is dict, return the dict. + If obj is swagger model, return the properties dict. + """ + if isinstance(obj, type(None)): return None - elif type(obj) in [str, int, long, float, bool]: + elif isinstance(obj, (str, int, long, float, bool, file)): return obj - elif type(obj) == list: - return [self.sanitizeForSerialization(subObj) for subObj in obj] - elif type(obj) == datetime.datetime: + elif isinstance(obj, list): + return [ApiClient.sanitizeForSerialization(subObj) for subObj in obj] + elif isinstance(obj, (datetime.datetime, datetime.date)): return obj.isoformat() else: - if type(obj) == dict: + if isinstance(obj, dict): objDict = obj else: - objDict = obj.__dict__ - return {key: self.sanitizeForSerialization(val) - for (key, val) in objDict.iteritems() - if key != 'swaggerTypes'} - - if type(postData) == list: - # Could be a list of objects - if type(postData[0]) in safeToDump: - data = json.dumps(postData) - else: - data = json.dumps([datum.__dict__ for datum in postData]) - elif type(postData) not in safeToDump: - data = json.dumps(postData.__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()} def buildMultipartFormData(self, postData, files): def escape_quotes(s): @@ -194,16 +198,13 @@ class ApiClient: if objClass in [int, long, float, dict, list, str, bool]: return objClass(obj) elif objClass == datetime: - # Server will always return a time stamp in UTC, but with - # trailing +0000 indicating no offset from UTC. So don't process - # last 5 characters. - return datetime.datetime.strptime(obj[:-5], "%Y-%m-%dT%H:%M:%S.%f") + return self.__parse_string_to_datetime(obj) 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: @@ -214,7 +215,7 @@ class ApiClient: value = value setattr(instance, attr, value) elif (attrType == 'datetime'): - setattr(instance, attr, datetime.datetime.strptime(value[:-5], "%Y-%m-%dT%H:%M:%S.%f")) + setattr(instance, attr, self.__parse_string_to_datetime(value)) elif 'list[' in attrType: match = re.match('list\[(.*)\]', attrType) subClass = match.group(1) @@ -226,10 +227,21 @@ class ApiClient: subValues.append(self.deserialize(subValue, subClass)) setattr(instance, attr, subValues) else: - setattr(instance, attr, self.deserialize(value, objClass)) + setattr(instance, attr, self.deserialize(value, attrType)) return instance + def __parse_string_to_datetime(self, string): + """ + Parse datetime in string to datetime. + + The string should be in iso8601 datetime format. + """ + try: + from dateutil.parser import parse + return parse(string) + except ImportError: + return string class MethodRequest(urllib2.Request): def __init__(self, *args, **kwargs): @@ -243,4 +255,4 @@ class MethodRequest(urllib2.Request): return urllib2.Request.__init__(self, *args, **kwargs) def get_method(self): - return getattr(self, 'method', urllib2.Request.get_method(self)) \ No newline at end of file + return getattr(self, 'method', urllib2.Request.get_method(self))