OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA][SpringCodegen] Inheritance copyOf() results in null parent attributes
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
Using inheritance via the normalizer setting
REF_AS_PARENT_IN_ALLOF
together with Builder generation, yields the following code for toBuilder methods
public Dog.Builder toBuilder() {
Dog.Builder builder = new Dog.Builder();
return builder.copyOf(this);
}
copyOf looks like this
protected Builder copyOf(Dog value) {
super.copyOf(instance);
this.instance.setIsGoodBoy(value.isGoodBoy);
return this;
}
Since super.copyOf(instance) is called instead of super.copyOf(value) we loose all set parent model values. instance is a fresh object without set values since the constructor was called in the .toBuilder() call.
public Builder() {
this(new Dog());
}
protected Builder(Dog instance) {
super(instance); // the parent builder shares the same instance
this.instance = instance;
}
Instead the copyOf method should look like this to retain all parent model values:
protected Builder copyOf(Dog value) {
super.copyOf(value);
this.instance.setIsGoodBoy(value.isGoodBoy);
return this;
}
openapi-generator version
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-08-22T10:43:42.285171+02:00[Europe/Berlin]", comments = "Generator version: 7.7.0")
Issue still persists in 7.8.0, since related .mustache file is still the same.
modules/openapi-generator/src/main/resources/JavaSpring/javaBuilder.mustache
Generated file example:
Dog.java
OpenAPI declaration file content or url
Generation Details
Builder Pattern generation enabled
Normalizer settings:
REF_AS_PARENT_IN_ALLOF
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/JavaSpring/javaBuilder.mustache and compare its generated copyOf method with the provided Dog.java example. Generate the models from Dog.yml with Builder generation and REF_AS_PARENT_IN_ALLOF enabled. Done means the generated child builder preserves parent model values when toBuilder and copyOf are used.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100