[JAVA] Cannot use single discriminator field for two-level inheritance
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start with the Java generator's pojo.mustache around the discriminator constructor shown in the issue, then generate the supplied two_level_inheritance.json example with the stated command. Inspect the generated ResponseBase and Response1 classes; done means the generated Maven project compiles and the shared discriminator works across both inheritance levels.
Written by the indexing model from the issue text.
Description
Description
I would like to use "inheritance" via allOf in the spec, and I'd like more than one level for my request schemas. This will allow me to have models all share a common base, but then further split them into categories. It seems that openapi-generator supports this, and validates such a spec, but the Java codegen does not produce valid code if the same discriminator field is used at both levels.
openapi-generator version
3.3.0-SNAPSHOT
OpenAPI declaration file content or url
{
"openapi": "3.0.0",
"info": {
"description": "blah",
"version": "1.0.0",
"title": "blah"
},
"paths": {
"/test1": {
"post": {
"tags": [
"test"
],
"operationId": "testOp1",
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"oneOf": [
{ "$ref": "#/components/schemas/Response1" },
{ "$ref": "#/components/schemas/Response2" }
]
}
}
}
},
"405": {
"description": "Invalid input"
}
},
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Request"
}
}
}
}
}
}
},
"components": {
"schemas": {
"Base": {
"properties": {
"type":{
"type":"string"
}
},
"discriminator":{
"propertyName": "type"
}
},
"Request": {
"allOf":[
{ "$ref": "#/components/schemas/Base" },
{
"type":"object",
"properties": {
"resource": {
"type": "string"
}
}
}
]
},
"ResponseBase": {
"allOf":[
{ "$ref": "#/components/schemas/Base" }
],
"discriminator":{
"propertyName": "type"
}
},
"Response1": {
"allOf":[
{ "$ref": "#/components/schemas/ResponseBase" },
{
"type":"object",
"properties": {
"status": {
"type": "string"
}
}
}
]
},
"Response2": {
"allOf":[
{ "$ref": "#/components/schemas/ResponseBase" },
{
"type":"object",
"properties": {
"name": {
"type": "string"
}
}
}
]
}
}
}
}
Command line used for generation
java -jar OPENAPIJAR.jar generate -o genjava -g java -i two_level_inheritance.json -c rpdmcpp.config
Steps to reproduce
Run above command and load generated maven project
Related issues/PRs
#1068 (CSharp generator)
Suggest a fix/enhancement
The problem is that the discriminator field is private in the base class, yet the intermediate class attempts to set it in the constructor:
public ResponseBase() {
this.type = this.getClass().getSimpleName();
}
There is no reason to when there is a parent using the same discriminator, because the parent (which has access to the field) will do it anyway. I believe this happens around line 81 of pojo.mustache:
{{^parcelableModel}}
{{#gson}}
{{#discriminator}}
public {{classname}}() {
this.{{{discriminatorName}}} = this.getClass().getSimpleName();
}
{{/discriminator}}
{{/gson}}
{{/parcelableModel}}
It may be impossible to condition mustache template on "if parent has the same discriminator field". However the setter is public. Can we know what is the setter for the discriminator? Can the fields be made protected instead of private?
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 100
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.
More from OpenAPITools/openapi-generator
-
Issue: Bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
OpenAPITools/openapi-generator#24859 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
OpenAPITools/openapi-generator#24852 · 1 comment ·
-
[BUG][KOTLIN] Fails to compile after updating to 7.25.0 with useJackson3=false useSpringBoot4=true OpenIssue: Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
OpenAPITools/openapi-generator#24842 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
OpenAPITools/openapi-generator#24830 ·
-
Issue: Bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
OpenAPITools/openapi-generator#24816 · 1 comment ·
All issues in OpenAPITools/openapi-generator
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
bug needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 1/5 Under an hour Newbie friendliness 94/100
objectionary/hone-maven-plugin#1061 ·
-
type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100