Open ka-zo opened 4 years ago
👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
An update to this issue: for versions 4.2.x and 4.3x of the OpenAPI Generator I have created my own class.mustache file including the following (not full) code snipet in the beginning of the class template:
class {{classname}} {
{{#vars}}
{{#description}}/* {{{description}}} */{{/description}}
{{#isPrimitiveType}}
{{{dataType}}} {{name}} = {{{defaultValue}}};
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{{dataType}}} {{name}} = new {{{dataType}}}();
{{/isPrimitiveType}}
{{#allowableValues}}
{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{
{{/allowableValues}}
{{/vars}}
{{classname}}();
The idea is that whenever the type of the parameter is complex, not a string, etc. but e.g. a class, then it gets instantiated. It was nicely working for me, for my OpenAPI json specification. Regarding the latest 5.0.0 beta releases, I do not expect changes in this regard. Instead of using my own class.mustache, my new solution for this is that my Dart code, that uses the Dart client code generated by the OpenAPI generator, needs to instantiate all classes that will be used as parameters in the generated classes. They need to be passed to the constructors of the generated classes, whenever I want to use them.
Bug Report Checklist
openapi-generator version
v4.2.2
OpenAPI declaration file content or url
My openapi.json specification:
My flutterconfig-dart.json file:
Command line used for generation
Steps to reproduce
Just generate the code using the command line, and then open any model dart files, such as: \openapi-test\lib\model\person.dart
There you can see that the constructor of the
Person
class isPerson();
So no instantiation of the fieldage
takes place.So in order to use the Person class you would need to do:
Imagine how big this problem is, if you wanted to instantiate a class with deeply nested field of other classes.
Suggest a fix
Extend all generated constructors to instantiate all fields.