OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.24k stars 6.43k forks source link

[BUG] [JAVA] Discriminator does not map objects that are inherited from the super class #18365

Open Oskarovsky opened 4 months ago

Oskarovsky commented 4 months ago

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?

Oskarovsky commented 4 months ago

Any updates? @wing328