OpenAPITools / OpenAPITools/openapi-generator

[REQ] generic return type for method chaining

Open
#14,023 0 comments 0 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

Is your feature request related to a problem? Please describe.

When I define a property "foo" on base type "Animal" and then create an instance of subtype "Dog", I can call dog.foo("value") but it returns the object as an Animal. Now I can't chain other properties of any subtypes.

Describe the solution you'd like

Return the object as the actual type.
At least add an override with a simple call to super.foo() and a type cast. That should be enough for most use cases.

Or make it like this in the base type (Java example):

public <T extends Animal> T foo(String foo){
  this.foo=foo;
  return (T)this;
}

And if T is actually a generic type parameter of the baseclass (i.e. Animal<T extends Animal>) it just works because T is always the correct type.

Describe alternatives you've considered

I didn't find any solution other than not using a discriminator and them ending up with no inheritence.

Additional context

Maybe there is a better solution. I didn't find anything on creating a builder instead or something else that handles this problem.

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

Start by tracing how Java models with inheritance and discriminators are generated, using the Animal, Dog, and foo example from the issue as the target case. Compare fluent setter return types with the proposed generic-base-type and override alternatives; done means a selected approach is defined and its generated output and compatibility implications are covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.