openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

Different handling of java Optional for models and APIs

Open arandth opened this issue 2 years ago • 3 comments

I was trying to update from openapi-generator 7.1.0 to 7.2.0. That causes problems due to a changed handling of useOptional. In 7.1.0, only methods in API's were treated (not attributes of the generated model classes.) With 7.2.0 that obviously changed, suddenly also the generated model classes have Optional fields (if they are not marked as required). One might argue that this is the correct behavior of the generator, but on the other hand it prevents us from updating - it would be a very hard work to change the implementation in either all APIs or all model classes.

So what we would need is to destinguish between APIs and models (such as "useOptionalInApi" / "useOptionalInModel" or similar). Is that already possible?

arandth avatar Jan 22 '24 15:01 arandth

Please note that I found a workaround: I simply invoked the generator twice (one call for API only (use-optional=true), the other one for the models only (use-optional=false). That works fine, even when invoked with the openapi-generator-maven-plugin

arandth avatar Jan 26 '24 16:01 arandth

Still facing the same issue in openapi-generator 7.5.0

yasstec avatar May 13 '24 15:05 yasstec

Hello, we really need this distinction. The PR https://github.com/OpenAPITools/openapi-generator/pull/17202, was made for its own purposes and as we can see in the thread, a lot of people are bothered by this MAJOR change. (even if I understand the arguments)

So I love the idea of ​​two "useOptionals", one for the API and one for the model. The work around works, but when you call many other RESTApis, doubling the generation becomes cumbersome and a waste of time. We still can use the openApiNullable, but it's hard to find in documentation and not sure it's 100% the same behaviour that before

UnleashSpirit avatar May 15 '24 12:05 UnleashSpirit