Python Client [v2.X] Added inheritance/polymorphism (allOf) to python client#11968
Python Client [v2.X] Added inheritance/polymorphism (allOf) to python client#11968mjschuetze102 wants to merge 4 commits intoswagger-api:masterfrom
Conversation
| @Override | ||
| protected void addImport(CodegenModel m, String type) { | ||
| if (m.parent != null && m.parent.equals(type)) { | ||
| super.addImport(m, type); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
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
| {{#allVars}} | ||
| '{{name}}': '{{{datatype}}}'{{#hasMore}},{{/hasMore}} | ||
| {{/vars}} | ||
| {{/allVars}} | ||
| } | ||
|
|
||
| attribute_map = { | ||
| {{#vars}} | ||
| {{#allVars}} | ||
| '{{name}}': '{{baseName}}'{{#hasMore}},{{/hasMore}} | ||
| {{/vars}} | ||
| {{/allVars}} |
There was a problem hiding this comment.
Due to the way the deserializer works in the api_client, the parent values must be present in swagger_types and attribute_map
| {{/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 |
There was a problem hiding this comment.
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
| {{#parent}} | ||
| super().__init__({{#parentModel.vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/parentModel.vars}}) # noqa: E501 | ||
|
|
||
| {{/parent}} |
There was a problem hiding this comment.
The parent constructor is called with the values passed in to the constructor of the child
There was a problem hiding this comment.
I believe this should go up to the first line below the init function so that calling the parent constructor does not wipe out any properties that were overridden by the current child class.
There was a problem hiding this comment.
I believe I had reasoned that it didn't matter much because the code was using vars for the child class properties and parentModel.vars for properties on the parent. Since the child class never worked with parentModel.vars directly it would be fine. However, the discriminator (and any other code that is added in the future) might not realize this, so I changed the order of events
…s aren't used in python models
|
Can this be merged? This would be extremely helpful for Python devs using this tool. |
Relocated the parent constructor call so that values set at the child level are not overwritten by values set at the parent level
PR checklist
./bin/to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.shand./bin/security/{LANG}-petstore.shif updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\.3.0.0branch for changes related to OpenAPI spec 3.0. Default:master.Description of the PR
This takes some of the changes made to OpenAPI v3.0 and expands on them to add inheritance/ polymorphism to Swagger 2.0
Fixes: #3904, #10912, #11225
Similar fixes could be applied to solve several issues for v3.X