Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Comment on lines +263 to +269
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only adds the parent class for importing as the python client does not need to know the type of the values it works with, but does need to import the parent class

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// process enum in models
Expand Down
20 changes: 13 additions & 7 deletions modules/swagger-codegen/src/main/resources/python/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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}}
Comment on lines +39 to +47
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the way the deserializer works in the api_client, the parent values must be present in swagger_types and attribute_map

}
{{#discriminator}}

Expand All @@ -52,8 +54,12 @@ class {{classname}}(object):
}
{{/discriminator}}

def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}, _configuration=None): # noqa: E501
"""{{classname}} - a model defined in Swagger""" # noqa: E501
def __init__(self{{#allVars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/allVars}}, _configuration=None): # noqa: E501
Copy link
Contributor Author

@mjschuetze102 mjschuetze102 Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly because the values for the model are defined with the constructor rather than using setters for each property, the parent vars must be passed in the constructor as well as the properties belonging to this model

"""{{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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down