OpenAPITools / OpenAPITools/openapi-generator

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

Open
#1,904 7 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no implementation files or tests; start with the Java model-generation path for schemas using allOf and the Foo/Bar example. Verify that generated Bar fluent setter calls can chain a("a").b("b") with a Bar result, and add or run coverage for this generated-model case if the repository provides a corresponding generator test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
backend-api-design, devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
56/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.