OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.66k stars 6.54k forks source link

[REQ][Java] Add covariant overrides for inherited allOf setters #1904

Open doctau opened 5 years ago

doctau commented 5 years ago

When generating a model class which is an allOf, the Java generators make it a subclass (of the first allOf entry anyway) with all the getter/setter methods inherited from the parent. This means that if you are using the fluent setters, the result of calling any inherited setter is the parent not the subclass. It would be useful if it returned the subclass.

For example, consider this fragment

    Foo:
      type: object
      properties:
        a:
          type: string
    Bar:
      allOf:
      - $ref: '#/components/schemas/Foo'
      - type: object
        properties:
          b:
            type: string

The following code will fail, because Bar.a() returns a Foo instead of a Bar. It would be safe to override the method to return Bar, since covariant return types are allowed in Java. Bar bar = new Bar().a("a").b("b")

This would make using the fluent setters easier when allOf is used.

wing328 commented 5 years ago

Which version are you using? Please try the latest master if you've not yet done so.

doctau commented 5 years ago

I was using 3.3.4, but I'll try master shortly. Thanks

sluglit commented 5 years ago

The problem still occurs in 4.0.0-SNAPSHOT

I wanted to create a custom pojo.mustache but model.parentVars is not set either

pixelshaded commented 4 years ago

I also attempted a custom template and parentVars was also empty

pixelshaded commented 4 years ago

isInherited seems like the proper boolean for this and could be used as a backup to parentVars, but it is false even in cases when the var comes from a parent.

ap190 commented 2 years ago

was this ever resolved?