OpenAPITools / OpenAPITools/openapi-generator
[BUG] [JAVA] Discriminator does not map objects that are inherited from the super class
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
used library: openapi-generator-cli-7.4.0
Description
I use annotations in a Spring application that are used to parameterize the objects used.
I have an abstract super class from which other objects inherit. I would like the response from the method generated using the generator to be of the appropriate type based on the defined parameter type.
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "childType")
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@Schema(
discriminatorMapping = {
@DiscriminatorMapping(value = "com.example.FirstChild", schema = FirstChild.class),
@DiscriminatorMapping(value = "com.example.SecondChild", schema = SecondChild.class),,
@DiscriminatorMapping(value = "com.example.ThirdChild", schema = ThirdChild.class)},
discriminatorProperty = "childType",
requiredProperties = {"childType"}
)
public abstract class ParentClass implements Serializable {
private String childType;
// getter, setter, constructor...
}
Here are some sample json file fragments which has been generated by swagger:
"ParentClass": {
"required": [
"childType"
],
"type": "object",
"properties": {
"childType": {
"type": "string"
}
},
"discriminator": {
"propertyName": "childType",
"mapping": {
"com.example.FirstChild": "#/components/schemas/FirstChild",
"com.example.SecondChild": "#/components/schemas/SecondChild",
"com.example.ThirdChild": "#/components/schemas/ThirdChild"
}
}
}
"SecondChild": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/ParentClass"
},
{
"type": "object",
"properties": {
"childType": {
"type": "string"
},
"currency": {
"type": "string"
},
"name": {
"type": "string"
},
"mrk": {
"type": "string"
}
}
}
]
}
Endpoint from spring controller
"/endpoint/test": {
"get": {
"description": "test description",
"operationId": "testId",
"parameters": [],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ParentClass"
}
}
}
}
Shouldn't this json file also contain the following fragment:
"type": "array" under "schema" ?
After running the method written in Python, instead of the response in json format, I get the following information:
[
InsertParentClassRequest(
oneof_schema_1_validator=None,
oneof_schema_2_validator=None,
oneof_schema_3_validator=None,
actual_instance=SecondChild(childType='com.example.SecondChild', currency='USD', name='RATE', mrk='9M'),
one_of_schemas=typing.Literal['FirstChild', 'SecondChild', 'ThirdChild'], discriminator_value_class_map={}),
InsertParentClassRequest(
oneof_schema_1_validator=None,
oneof_schema_2_validator=None,
oneof_schema_3_validator=None,
actual_instance=SecondChild(childType='com.example.SecondChild', currency='PLN', name='XXX', mrk='2M'),
one_of_schemas=typing.Literal['FirstChild', 'SecondChild', 'ThirdChild'], discriminator_value_class_map={})
]
The python code generates without errors and the vast majority of classes work flawlessly. However, we have a problem with class inheritance. I have already tried many ways, but nothing brought the expected result.
Do you have any idea what's wrong?
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
No source files or tests are named. Start by reproducing the OpenAPI schema from the ParentClass and SecondChild examples with openapi-generator-cli 7.4.0, then inspect the generated Python models and endpoint response handling; done means inherited discriminator values map to the appropriate child class and the expected response shape is established.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, python, spring
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100