Add polymorphism support for python (#6066)

This commit is contained in:
Jeff 2017-07-23 09:25:39 -07:00 committed by wing328
parent 4a8d9c3509
commit 67288b1303
2 changed files with 22 additions and 1 deletions

View File

@ -41,7 +41,8 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
// clear import mapping (from default generator) as python does not use it
// at the moment
importMapping.clear();
supportsInheritance = true;
modelPackage = "models";
apiPackage = "api";
outputFolder = "generated-code" + File.separatorChar + "python";

View File

@ -40,6 +40,13 @@ class {{classname}}(object):
{{#vars}}'{{name}}': '{{baseName}}'{{#hasMore}},
{{/hasMore}}{{/vars}}
}
{{#discriminator}}
discriminator_value_class_map = {
{{#children}}'{{vendorExtensions.x-discriminator-value}}': '{{{classname}}}'{{^-last}},
{{/-last}}{{/children}}
}
{{/discriminator}}
def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}):
"""
@ -49,6 +56,7 @@ class {{classname}}(object):
{{#vars}}
self._{{name}} = None
{{/vars}}
self.discriminator = {{#discriminator}}'{{discriminator}}'{{/discriminator}}{{^discriminator}}None{{/discriminator}}
{{#vars}}
{{#required}}
@ -154,6 +162,18 @@ class {{classname}}(object):
self._{{name}} = {{name}}
{{/vars}}
{{#discriminator}}
def get_real_child_model(self, data):
"""
Returns the real base class specified by the discriminator
"""
discriminator_value = data[self.discriminator].lower()
if self.discriminator_value_class_map.has_key(discriminator_value):
return self.discriminator_value_class_map[discriminator_value]
else:
return None
{{/discriminator}}
def to_dict(self):
"""
Returns the model properties as a dict