Skip to content

Commit 01d0b5d

Browse files
gtarcodersunkaicheng
andauthored
added serialize option in to_dict function in python generated model (#7555)
* added to_json_dict in python generated model * update to_dict * updated to_dict Co-authored-by: sunkaicheng <sunkaicheng@bigo.sg>
1 parent b3bc926 commit 01d0b5d

File tree

194 files changed

+3104
-1552
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

194 files changed

+3104
-1552
lines changed

modules/openapi-generator/src/main/resources/python/model.mustache

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
{{>partial_header}}
44

5+
import inspect
56
import pprint
67
import re # noqa: F401
7-
88
import six
99

1010
from {{packageName}}.configuration import Configuration
@@ -197,27 +197,35 @@ class {{classname}}(object):
197197
return self.discriminator_value_class_map.get(discriminator_value)
198198

199199
{{/discriminator}}
200-
def to_dict(self):
200+
def to_dict(self, serialize=False):
201201
"""Returns the model properties as a dict"""
202202
result = {}
203203

204+
def convert(x):
205+
if hasattr(x, "to_dict"):
206+
args = inspect.getargspec(x.to_dict).args
207+
if len(args) == 1:
208+
return x.to_dict()
209+
else:
210+
return x.to_dict(serialize)
211+
else:
212+
return x
213+
204214
for attr, _ in six.iteritems(self.openapi_types):
205215
value = getattr(self, attr)
216+
attr = self.attribute_map.get(attr, attr) if serialize else attr
206217
if isinstance(value, list):
207218
result[attr] = list(map(
208-
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
219+
lambda x: convert(x),
209220
value
210221
))
211-
elif hasattr(value, "to_dict"):
212-
result[attr] = value.to_dict()
213222
elif isinstance(value, dict):
214223
result[attr] = dict(map(
215-
lambda item: (item[0], item[1].to_dict())
216-
if hasattr(item[1], "to_dict") else item,
224+
lambda item: (item[0], convert(item[1])),
217225
value.items()
218226
))
219227
else:
220-
result[attr] = value
228+
result[attr] = convert(value)
221229

222230
return result
223231

samples/client/petstore/python-asyncio/petstore_api/models/additional_properties_any_type.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"""
1111

1212

13+
import inspect
1314
import pprint
1415
import re # noqa: F401
15-
1616
import six
1717

1818
from petstore_api.configuration import Configuration
@@ -73,27 +73,35 @@ def name(self, name):
7373

7474
self._name = name
7575

76-
def to_dict(self):
76+
def to_dict(self, serialize=False):
7777
"""Returns the model properties as a dict"""
7878
result = {}
7979

80+
def convert(x):
81+
if hasattr(x, "to_dict"):
82+
args = inspect.getargspec(x.to_dict).args
83+
if len(args) == 1:
84+
return x.to_dict()
85+
else:
86+
return x.to_dict(serialize)
87+
else:
88+
return x
89+
8090
for attr, _ in six.iteritems(self.openapi_types):
8191
value = getattr(self, attr)
92+
attr = self.attribute_map.get(attr, attr) if serialize else attr
8293
if isinstance(value, list):
8394
result[attr] = list(map(
84-
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
95+
lambda x: convert(x),
8596
value
8697
))
87-
elif hasattr(value, "to_dict"):
88-
result[attr] = value.to_dict()
8998
elif isinstance(value, dict):
9099
result[attr] = dict(map(
91-
lambda item: (item[0], item[1].to_dict())
92-
if hasattr(item[1], "to_dict") else item,
100+
lambda item: (item[0], convert(item[1])),
93101
value.items()
94102
))
95103
else:
96-
result[attr] = value
104+
result[attr] = convert(value)
97105

98106
return result
99107

samples/client/petstore/python-asyncio/petstore_api/models/additional_properties_array.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"""
1111

1212

13+
import inspect
1314
import pprint
1415
import re # noqa: F401
15-
1616
import six
1717

1818
from petstore_api.configuration import Configuration
@@ -73,27 +73,35 @@ def name(self, name):
7373

7474
self._name = name
7575

76-
def to_dict(self):
76+
def to_dict(self, serialize=False):
7777
"""Returns the model properties as a dict"""
7878
result = {}
7979

80+
def convert(x):
81+
if hasattr(x, "to_dict"):
82+
args = inspect.getargspec(x.to_dict).args
83+
if len(args) == 1:
84+
return x.to_dict()
85+
else:
86+
return x.to_dict(serialize)
87+
else:
88+
return x
89+
8090
for attr, _ in six.iteritems(self.openapi_types):
8191
value = getattr(self, attr)
92+
attr = self.attribute_map.get(attr, attr) if serialize else attr
8293
if isinstance(value, list):
8394
result[attr] = list(map(
84-
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
95+
lambda x: convert(x),
8596
value
8697
))
87-
elif hasattr(value, "to_dict"):
88-
result[attr] = value.to_dict()
8998
elif isinstance(value, dict):
9099
result[attr] = dict(map(
91-
lambda item: (item[0], item[1].to_dict())
92-
if hasattr(item[1], "to_dict") else item,
100+
lambda item: (item[0], convert(item[1])),
93101
value.items()
94102
))
95103
else:
96-
result[attr] = value
104+
result[attr] = convert(value)
97105

98106
return result
99107

samples/client/petstore/python-asyncio/petstore_api/models/additional_properties_boolean.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"""
1111

1212

13+
import inspect
1314
import pprint
1415
import re # noqa: F401
15-
1616
import six
1717

1818
from petstore_api.configuration import Configuration
@@ -73,27 +73,35 @@ def name(self, name):
7373

7474
self._name = name
7575

76-
def to_dict(self):
76+
def to_dict(self, serialize=False):
7777
"""Returns the model properties as a dict"""
7878
result = {}
7979

80+
def convert(x):
81+
if hasattr(x, "to_dict"):
82+
args = inspect.getargspec(x.to_dict).args
83+
if len(args) == 1:
84+
return x.to_dict()
85+
else:
86+
return x.to_dict(serialize)
87+
else:
88+
return x
89+
8090
for attr, _ in six.iteritems(self.openapi_types):
8191
value = getattr(self, attr)
92+
attr = self.attribute_map.get(attr, attr) if serialize else attr
8293
if isinstance(value, list):
8394
result[attr] = list(map(
84-
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
95+
lambda x: convert(x),
8596
value
8697
))
87-
elif hasattr(value, "to_dict"):
88-
result[attr] = value.to_dict()
8998
elif isinstance(value, dict):
9099
result[attr] = dict(map(
91-
lambda item: (item[0], item[1].to_dict())
92-
if hasattr(item[1], "to_dict") else item,
100+
lambda item: (item[0], convert(item[1])),
93101
value.items()
94102
))
95103
else:
96-
result[attr] = value
104+
result[attr] = convert(value)
97105

98106
return result
99107

samples/client/petstore/python-asyncio/petstore_api/models/additional_properties_class.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"""
1111

1212

13+
import inspect
1314
import pprint
1415
import re # noqa: F401
15-
1616
import six
1717

1818
from petstore_api.configuration import Configuration
@@ -333,27 +333,35 @@ def anytype_3(self, anytype_3):
333333

334334
self._anytype_3 = anytype_3
335335

336-
def to_dict(self):
336+
def to_dict(self, serialize=False):
337337
"""Returns the model properties as a dict"""
338338
result = {}
339339

340+
def convert(x):
341+
if hasattr(x, "to_dict"):
342+
args = inspect.getargspec(x.to_dict).args
343+
if len(args) == 1:
344+
return x.to_dict()
345+
else:
346+
return x.to_dict(serialize)
347+
else:
348+
return x
349+
340350
for attr, _ in six.iteritems(self.openapi_types):
341351
value = getattr(self, attr)
352+
attr = self.attribute_map.get(attr, attr) if serialize else attr
342353
if isinstance(value, list):
343354
result[attr] = list(map(
344-
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
355+
lambda x: convert(x),
345356
value
346357
))
347-
elif hasattr(value, "to_dict"):
348-
result[attr] = value.to_dict()
349358
elif isinstance(value, dict):
350359
result[attr] = dict(map(
351-
lambda item: (item[0], item[1].to_dict())
352-
if hasattr(item[1], "to_dict") else item,
360+
lambda item: (item[0], convert(item[1])),
353361
value.items()
354362
))
355363
else:
356-
result[attr] = value
364+
result[attr] = convert(value)
357365

358366
return result
359367

samples/client/petstore/python-asyncio/petstore_api/models/additional_properties_integer.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"""
1111

1212

13+
import inspect
1314
import pprint
1415
import re # noqa: F401
15-
1616
import six
1717

1818
from petstore_api.configuration import Configuration
@@ -73,27 +73,35 @@ def name(self, name):
7373

7474
self._name = name
7575

76-
def to_dict(self):
76+
def to_dict(self, serialize=False):
7777
"""Returns the model properties as a dict"""
7878
result = {}
7979

80+
def convert(x):
81+
if hasattr(x, "to_dict"):
82+
args = inspect.getargspec(x.to_dict).args
83+
if len(args) == 1:
84+
return x.to_dict()
85+
else:
86+
return x.to_dict(serialize)
87+
else:
88+
return x
89+
8090
for attr, _ in six.iteritems(self.openapi_types):
8191
value = getattr(self, attr)
92+
attr = self.attribute_map.get(attr, attr) if serialize else attr
8293
if isinstance(value, list):
8394
result[attr] = list(map(
84-
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
95+
lambda x: convert(x),
8596
value
8697
))
87-
elif hasattr(value, "to_dict"):
88-
result[attr] = value.to_dict()
8998
elif isinstance(value, dict):
9099
result[attr] = dict(map(
91-
lambda item: (item[0], item[1].to_dict())
92-
if hasattr(item[1], "to_dict") else item,
100+
lambda item: (item[0], convert(item[1])),
93101
value.items()
94102
))
95103
else:
96-
result[attr] = value
104+
result[attr] = convert(value)
97105

98106
return result
99107

samples/client/petstore/python-asyncio/petstore_api/models/additional_properties_number.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"""
1111

1212

13+
import inspect
1314
import pprint
1415
import re # noqa: F401
15-
1616
import six
1717

1818
from petstore_api.configuration import Configuration
@@ -73,27 +73,35 @@ def name(self, name):
7373

7474
self._name = name
7575

76-
def to_dict(self):
76+
def to_dict(self, serialize=False):
7777
"""Returns the model properties as a dict"""
7878
result = {}
7979

80+
def convert(x):
81+
if hasattr(x, "to_dict"):
82+
args = inspect.getargspec(x.to_dict).args
83+
if len(args) == 1:
84+
return x.to_dict()
85+
else:
86+
return x.to_dict(serialize)
87+
else:
88+
return x
89+
8090
for attr, _ in six.iteritems(self.openapi_types):
8191
value = getattr(self, attr)
92+
attr = self.attribute_map.get(attr, attr) if serialize else attr
8293
if isinstance(value, list):
8394
result[attr] = list(map(
84-
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
95+
lambda x: convert(x),
8596
value
8697
))
87-
elif hasattr(value, "to_dict"):
88-
result[attr] = value.to_dict()
8998
elif isinstance(value, dict):
9099
result[attr] = dict(map(
91-
lambda item: (item[0], item[1].to_dict())
92-
if hasattr(item[1], "to_dict") else item,
100+
lambda item: (item[0], convert(item[1])),
93101
value.items()
94102
))
95103
else:
96-
result[attr] = value
104+
result[attr] = convert(value)
97105

98106
return result
99107

0 commit comments

Comments
 (0)