From 6e72018a6415387d2f3b0500db1dccbf4f9bcedf Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 2 Aug 2017 02:15:26 -0700 Subject: [PATCH] Add polymorphism support for python (#6208) * Add polymorphism support for python * Update api_client * Remove leading spaces * Bugfix * Catch case where swagger_types is none but get_real_child_model is defined --- .../languages/PythonClientCodegen.java | 2 +- .../main/resources/python/api_client.mustache | 22 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java index f91c4fcc87f..21e76d9f265 100755 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java @@ -41,7 +41,7 @@ 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"; diff --git a/modules/swagger-codegen/src/main/resources/python/api_client.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache index e26a5ac3112..1a6ecf25db3 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -593,17 +593,23 @@ class ApiClient(object): :param klass: class literal. :return: model object. """ - if not klass.swagger_types: + + if not klass.swagger_types and not hasattr(klass, 'get_real_child_model'): return data kwargs = {} - for attr, attr_type in iteritems(klass.swagger_types): - if data is not None \ - and klass.attribute_map[attr] in data \ - and isinstance(data, (list, dict)): - value = data[klass.attribute_map[attr]] - kwargs[attr] = self.__deserialize(value, attr_type) + if klass.swagger_types is not None: + for attr, attr_type in iteritems(klass.swagger_types): + if data is not None \ + and klass.attribute_map[attr] in data \ + and isinstance(data, (list, dict)): + value = data[klass.attribute_map[attr]] + kwargs[attr] = self.__deserialize(value, attr_type) - instance = klass(**kwargs) + instance = klass(**kwargs) + if hasattr(instance, 'get_real_child_model'): + klass_name = instance.get_real_child_model(data) + if klass_name: + instance = self.__deserialize(data, klass_name) return instance