quarkiverse / quarkus-openapi-generator

OpenAPI Generator - REST Client Generator
Apache License 2.0
119 stars 80 forks source link

Issue with "Add" to Containers, missing initialization for non requiered containers. #651

Closed boyi01 closed 7 months ago

boyi01 commented 7 months ago

Hello

I tried to use our openapi Generator with this version, because it fits our needs very well and we use Quarkus.

When in the model there is a field, that is for example a list and is not diffined as requierd in the yaml, then in the add method there is no initialization, if this field is null.

For requierd fields this no problem because the default openapi-generator paramter for the config containerDefaultToNull is false, which results in a default initialization. For non requierd fields this is not happening.

I'm surprised because in all openapi java pojo.mustache the template is (in the microprofile too) somehow like this public {{classname}} add{{nameInCamelCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) { if (this.{{name}} == null) { this.{{name}} = {{{defaultValue}}}{{^defaultValue}}new {{#uniqueItems}}LinkedHashSet{{/uniqueItems}}{{^uniqueItems}}ArrayList{{/uniqueItems}}<>(){{/defaultValue}}; } this.{{name}}.add({{name}}Item); return this; } I'm kind of confused why the initialization part can't be found in the generated code.

Can somebody give a hint?

I'm open to constribute a solution, if this bevaivor is an error.

ricardozanini commented 7 months ago

@boyi01 feel free to send a PR to fix this initialization procedure for non-required fields.

boyi01 commented 7 months ago

@ricardozanini Before i go deeply into this problem, is my assumption correct that the quarkus-openapi-generator is using the openapi java generator or do we use the javajxRS or some other generator?

ricardozanini commented 7 months ago

We use the openapi java generator under the hood, but with our own configurations and templates to match Quarkus ecosystem. The idea is to change to apicurio generator in the future.

boyi01 commented 7 months ago

@ricardozanini Thank you, now i found the place of the Problem.

The critical question now is, how to fix Problem, because in my opinion there two options:

  1. Fix it as openapi does it, in each "add" method for a container, generate a check if the container is null, if it's null instantiate a new empty container. --> Here of course we maybe need a parameter? or not?
  2. Give the possibility to activate initialization for non required fields via parameter?

I prefer option 1, cause it's similar to openapi. I'm not sure if a parameter is needed, because it should not break anything...

ricardozanini commented 7 months ago

I'd go for option 1 too, shouldn't break anything :)