From 2ddbfa821856a51280703635a03f06a25cf59f1e Mon Sep 17 00:00:00 2001 From: Matan Goldman Date: Sun, 7 Feb 2016 18:05:07 +0200 Subject: [PATCH 1/2] Now it should be fully compatible with python 2.7 and 3.4 --- .../swagger-codegen/src/main/resources/python/model.mustache | 5 +++-- .../client/petstore/python/swagger_client/models/category.py | 5 +++-- .../client/petstore/python/swagger_client/models/order.py | 5 +++-- samples/client/petstore/python/swagger_client/models/pet.py | 5 +++-- samples/client/petstore/python/swagger_client/models/tag.py | 5 +++-- samples/client/petstore/python/swagger_client/models/user.py | 5 +++-- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index d7c3493d9da..f14a121aef9 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -97,8 +97,9 @@ class {{classname}}(object): )) elif isinstance(value, dict): result[attr] = dict(map( - lambda k, v: (k, v.to_dict()) if hasattr(v, "to_dict") else (k, v), - value.iteritems() + lambda item: (item[0], item[1].to_dict()) + if hasattr(item[1], "to_dict") else item, + value.items() )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() diff --git a/samples/client/petstore/python/swagger_client/models/category.py b/samples/client/petstore/python/swagger_client/models/category.py index 59970e0bc3f..9f66c3e6764 100644 --- a/samples/client/petstore/python/swagger_client/models/category.py +++ b/samples/client/petstore/python/swagger_client/models/category.py @@ -108,8 +108,9 @@ def to_dict(self): )) elif isinstance(value, dict): result[attr] = dict(map( - lambda k, v: (k, v.to_dict()) if hasattr(v, "to_dict") else (k, v), - value.iteritems() + lambda item: (item[0], item[1].to_dict()) + if hasattr(item[1], "to_dict") else item, + value.items() )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() diff --git a/samples/client/petstore/python/swagger_client/models/order.py b/samples/client/petstore/python/swagger_client/models/order.py index 488f01f05ba..b2b490ddfda 100644 --- a/samples/client/petstore/python/swagger_client/models/order.py +++ b/samples/client/petstore/python/swagger_client/models/order.py @@ -214,8 +214,9 @@ def to_dict(self): )) elif isinstance(value, dict): result[attr] = dict(map( - lambda k, v: (k, v.to_dict()) if hasattr(v, "to_dict") else (k, v), - value.iteritems() + lambda item: (item[0], item[1].to_dict()) + if hasattr(item[1], "to_dict") else item, + value.items() )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() diff --git a/samples/client/petstore/python/swagger_client/models/pet.py b/samples/client/petstore/python/swagger_client/models/pet.py index 94ca2249c0a..a568235a492 100644 --- a/samples/client/petstore/python/swagger_client/models/pet.py +++ b/samples/client/petstore/python/swagger_client/models/pet.py @@ -214,8 +214,9 @@ def to_dict(self): )) elif isinstance(value, dict): result[attr] = dict(map( - lambda k, v: (k, v.to_dict()) if hasattr(v, "to_dict") else (k, v), - value.iteritems() + lambda item: (item[0], item[1].to_dict()) + if hasattr(item[1], "to_dict") else item, + value.items() )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() diff --git a/samples/client/petstore/python/swagger_client/models/tag.py b/samples/client/petstore/python/swagger_client/models/tag.py index 4deced8a351..293b13f51ad 100644 --- a/samples/client/petstore/python/swagger_client/models/tag.py +++ b/samples/client/petstore/python/swagger_client/models/tag.py @@ -108,8 +108,9 @@ def to_dict(self): )) elif isinstance(value, dict): result[attr] = dict(map( - lambda k, v: (k, v.to_dict()) if hasattr(v, "to_dict") else (k, v), - value.iteritems() + lambda item: (item[0], item[1].to_dict()) + if hasattr(item[1], "to_dict") else item, + value.items() )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() diff --git a/samples/client/petstore/python/swagger_client/models/user.py b/samples/client/petstore/python/swagger_client/models/user.py index f8c1012842a..6a161d8cbbf 100644 --- a/samples/client/petstore/python/swagger_client/models/user.py +++ b/samples/client/petstore/python/swagger_client/models/user.py @@ -258,8 +258,9 @@ def to_dict(self): )) elif isinstance(value, dict): result[attr] = dict(map( - lambda k, v: (k, v.to_dict()) if hasattr(v, "to_dict") else (k, v), - value.iteritems() + lambda item: (item[0], item[1].to_dict()) + if hasattr(item[1], "to_dict") else item, + value.items() )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() From 341f862569c56a69a6f5747048491d5e3f27d8db Mon Sep 17 00:00:00 2001 From: Matan Goldman Date: Sun, 7 Feb 2016 20:42:24 +0200 Subject: [PATCH 2/2] change the order of the elif inside to_dict for better performance (dictionary/additionalProperties is less probable) --- .../swagger-codegen/src/main/resources/python/model.mustache | 4 ++-- .../client/petstore/python/swagger_client/models/category.py | 4 ++-- samples/client/petstore/python/swagger_client/models/order.py | 4 ++-- samples/client/petstore/python/swagger_client/models/pet.py | 4 ++-- samples/client/petstore/python/swagger_client/models/tag.py | 4 ++-- samples/client/petstore/python/swagger_client/models/user.py | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index f14a121aef9..bd33a997c3e 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -95,14 +95,14 @@ class {{classname}}(object): lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) - elif hasattr(value, "to_dict"): - result[attr] = value.to_dict() else: result[attr] = value diff --git a/samples/client/petstore/python/swagger_client/models/category.py b/samples/client/petstore/python/swagger_client/models/category.py index 9f66c3e6764..b8d540c51bd 100644 --- a/samples/client/petstore/python/swagger_client/models/category.py +++ b/samples/client/petstore/python/swagger_client/models/category.py @@ -106,14 +106,14 @@ def to_dict(self): lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) - elif hasattr(value, "to_dict"): - result[attr] = value.to_dict() else: result[attr] = value diff --git a/samples/client/petstore/python/swagger_client/models/order.py b/samples/client/petstore/python/swagger_client/models/order.py index b2b490ddfda..4c8911f267b 100644 --- a/samples/client/petstore/python/swagger_client/models/order.py +++ b/samples/client/petstore/python/swagger_client/models/order.py @@ -212,14 +212,14 @@ def to_dict(self): lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) - elif hasattr(value, "to_dict"): - result[attr] = value.to_dict() else: result[attr] = value diff --git a/samples/client/petstore/python/swagger_client/models/pet.py b/samples/client/petstore/python/swagger_client/models/pet.py index a568235a492..dfa614b1e9b 100644 --- a/samples/client/petstore/python/swagger_client/models/pet.py +++ b/samples/client/petstore/python/swagger_client/models/pet.py @@ -212,14 +212,14 @@ def to_dict(self): lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) - elif hasattr(value, "to_dict"): - result[attr] = value.to_dict() else: result[attr] = value diff --git a/samples/client/petstore/python/swagger_client/models/tag.py b/samples/client/petstore/python/swagger_client/models/tag.py index 293b13f51ad..50f829d65e3 100644 --- a/samples/client/petstore/python/swagger_client/models/tag.py +++ b/samples/client/petstore/python/swagger_client/models/tag.py @@ -106,14 +106,14 @@ def to_dict(self): lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) - elif hasattr(value, "to_dict"): - result[attr] = value.to_dict() else: result[attr] = value diff --git a/samples/client/petstore/python/swagger_client/models/user.py b/samples/client/petstore/python/swagger_client/models/user.py index 6a161d8cbbf..fd6d16824b3 100644 --- a/samples/client/petstore/python/swagger_client/models/user.py +++ b/samples/client/petstore/python/swagger_client/models/user.py @@ -256,14 +256,14 @@ def to_dict(self): lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) + elif hasattr(value, "to_dict"): + result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) - elif hasattr(value, "to_dict"): - result[attr] = value.to_dict() else: result[attr] = value