OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA][SPRING] required fields in subclass not set in constructor
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When using inheritance, if I have a field that is required on a subclass but not required on the super class then the generated constructor does not provide what we need.
The sublass constructor calls the super class constructor with the required super class fields but never sets the required subclass field and so it is always null.
openapi-generator version
7.11.0
OpenAPI declaration file content or url
components:
schemas:
Animal:
type: object
x-parent: true
properties:
id:
type: string
name:
type: string
breed:
type: string
required:
- name
- breed
AnimalCreate:
allOf:
- $ref: "#/components/schemas/Animal"
not:
required:
- id
AnimalResponse:
allOf:
- $ref: "#/components/schemas/Animal"
required:
- id
Generation Details
Generation options
configOptions = [
"basePackage": "com.animal.generated",
"configPackage": "com.animal.generated.config",
"dateLibrary": "java8",
"disallowAdditionalPropertiesIfNotPresent": "true",
"generateBuilders": "true",
"generateConstructorWithAllArgs": "true",
"groupId": "com.animal",
"interfaceOnly": "true",
"openApiNullable": "false",
"performBeanValidation": "true",
"requestMappingMode": "controller",
"skipDefaultInterface": "true",
"singleContentTypes": "true",
"useEnumCaseInsensitive": "true",
"useOneOfInterfaces": "true",
"useOptional": "true",
"useSpringBoot3": "true"
]
Generated code
In the parent Animal class:
/**
* Constructor with only required parameters
*/
public Animal(String name, String breed) {
this.name = name;
this.breed = breed;
}
/**
* Constructor with all args parameters
*/
public Animal(String id, String name, String breed) {
this.id = id;
this.name = name;
this.breed = breed;
}
In the AnimalResponse subclass:
/**
* Constructor with only required parameters and all parameters
*/
public AnimalResponse(String id, String name, String breed) {
super(name, breed);
}
As you can see the String id is not set using the constructor and so always ends up null.
Steps to reproduce
Create an API:
paths:
$ref: "index.yaml#/paths/~1animals"
with a path:
paths:
"/animals":
get:
operationId: getAnimal
summary: Returns an Animal
description: Returns an Animal
security:
- { }
responses:
200:
$ref: "#/components/responses/animal"
tags:
- animals
and response
animal:
description: "Animal"
content:
application/json:
schema:
$ref: "#/components/schemas/AnimalResponse"
that uses component described above.
After generating, look at the generated class and observe that the constructor has a parameter which is never used.
Related issues/PRs
Couldn't find one
Suggest a fix
I think it should generate as:
/**
* Constructor with only required parameters and all parameters
*/
public AnimalResponse(String id, String name, String breed) {
super(name, breed);
super.id(id)
}
I am willing to help with fixing this, I want to confirm that it is a bug first.
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
Reproduce the issue with the supplied Animal, AnimalCreate, and AnimalResponse schemas and the listed Java Spring generation options, then inspect how the generated AnimalResponse constructor handles inherited required fields. Done means the constructor uses every parameter, including id, while still invoking the parent constructor correctly; verify the generated output against the shown failure case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend-api-design, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100