From 00fdb1a7211391aa366c37861de155513ca4569f Mon Sep 17 00:00:00 2001 From: Michael Schuetze Date: Fri, 11 Nov 2022 16:36:21 -0600 Subject: [PATCH 1/3] Added inheritance [allOf] to python client --- .../codegen/languages/PythonClientCodegen.java | 7 +++++++ .../src/main/resources/python/model.mustache | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 6 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 9ef2e69e009..480291e5827 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 @@ -260,6 +260,13 @@ public String toModelImport(String name) { return modelImport; } + @Override + protected void addImport(CodegenModel m, String type) { + if (m.parent != null && m.parent.equals(type)) { + super.addImport(m, type); + } + } + @Override public Map postProcessModels(Map objs) { // process enum in models diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index c7686b57042..c451d50b15a 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -6,13 +6,15 @@ import pprint import re # noqa: F401 import six +{{#imports}}{{import}} # noqa: F401,E501 +{{/imports}} from {{packageName}}.configuration import Configuration {{#models}} {{#model}} -class {{classname}}(object): +class {{classname}}({{#parent}}{{.}}{{/parent}}{{^parent}}object{{/parent}}): """NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. @@ -34,15 +36,15 @@ class {{classname}}(object): and the value is json key in definition. """ swagger_types = { -{{#vars}} +{{#allVars}} '{{name}}': '{{{datatype}}}'{{#hasMore}},{{/hasMore}} -{{/vars}} +{{/allVars}} } attribute_map = { -{{#vars}} +{{#allVars}} '{{name}}': '{{baseName}}'{{#hasMore}},{{/hasMore}} -{{/vars}} +{{/allVars}} } {{#discriminator}} @@ -52,7 +54,7 @@ class {{classname}}(object): } {{/discriminator}} - def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}, _configuration=None): # noqa: E501 + def __init__(self{{#allVars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/allVars}}, _configuration=None): # noqa: E501 """{{classname}} - a model defined in Swagger""" # noqa: E501 if _configuration is None: _configuration = Configuration() @@ -73,6 +75,10 @@ class {{classname}}(object): {{/required}} {{/vars}} +{{#parent}} + super().__init__({{#parentModel.vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/parentModel.vars}}) # noqa: E501 + +{{/parent}} {{#vars}} @property def {{name}}(self): From cb86e8de338d1a39192a325faad633b7ce3271d9 Mon Sep 17 00:00:00 2001 From: Michael Schuetze Date: Fri, 11 Nov 2022 17:39:08 -0600 Subject: [PATCH 2/3] Removed import requirement on python complexMapPropertyTest as imports aren't used in python models --- .../src/test/java/io/swagger/codegen/python/PythonTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/python/PythonTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/python/PythonTest.java index cde532e6135..698a7b751e2 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/python/PythonTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/python/PythonTest.java @@ -226,7 +226,6 @@ public void complexMapPropertyTest() { Assert.assertEquals(cm.classname, "Sample"); Assert.assertEquals(cm.description, "a sample model"); Assert.assertEquals(cm.vars.size(), 1); - Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1); final CodegenProperty property1 = cm.vars.get(0); Assert.assertEquals(property1.baseName, "children"); From bb5fe7793c17852ff6a89b9237598d6f4c435411 Mon Sep 17 00:00:00 2001 From: mjschuetze102 Date: Sat, 21 Jan 2023 11:50:32 -0500 Subject: [PATCH 3/3] Relocated parent constructor call to top of init Relocated the parent constructor call so that values set at the child level are not overwritten by values set at the parent level --- .../src/main/resources/python/model.mustache | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index c451d50b15a..ee95f5335f0 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -55,7 +55,11 @@ class {{classname}}({{#parent}}{{.}}{{/parent}}{{^parent}}object{{/parent}}): {{/discriminator}} def __init__(self{{#allVars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/allVars}}, _configuration=None): # noqa: E501 - """{{classname}} - a model defined in Swagger""" # noqa: E501 + """{{classname}} - a model defined in Swagger""" # noqa: E501 +{{#parent}} + super().__init__({{#parentModel.vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/parentModel.vars}}) # noqa: E501 + +{{/parent}} if _configuration is None: _configuration = Configuration() self._configuration = _configuration @@ -75,10 +79,6 @@ class {{classname}}({{#parent}}{{.}}{{/parent}}{{^parent}}object{{/parent}}): {{/required}} {{/vars}} -{{#parent}} - super().__init__({{#parentModel.vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/parentModel.vars}}) # noqa: E501 - -{{/parent}} {{#vars}} @property def {{name}}(self):