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.23k stars 6.43k forks source link

[BUG] [JAVA] Use Object for unsupported OneOf cases #16087

Open mvanassche opened 1 year ago

mvanassche commented 1 year ago
Description

The generator generates unusable java code for complex expressions. For example, take a oneOf that is either a string or some complex object. The result is a an interface, which does not allow to read/write all possible values.

openapi-generator version

6.6.0

OpenAPI declaration file content or url
{
    "openapi": "3.0.1",
    "info": {
        "title": "OpenAPI definition",
        "version": "v0"
    },
    "components": {
        "schemas": {
            "Literal": {
                "type": "object",
                "oneOf": [
                    {
                        "type": "string"
                    },
                    {
                        "type": "object",
                        "properties": {
                            "@value": {
                                "type": "string"
                            },
                            "@type": {
                                "type": "string",
                                "example": "xsd:decimal"
                            },
                            "@language": {
                                "type": "string",
                                "example": "en"
                            }
                        }
                    }
                ]
            }
        }
    }
}
Suggest a fix

Shouldn't it fallback on an Object or Object Map instead of generating something incorrect?

wing328 commented 1 year ago

can you please try the latest master? we recently merged a PR to better handle oneOf in java client generator.

dzlab commented 10 months ago

@wing328 currently using latest version 7.0.1 in Java 11 and I get un-usable code when there is oneOf in the spec.

For instance something like this schema

    Parent:
      type: object
      properties:
        content:
          nullable: true
          oneOf:
            - type: string
            - type: array
              items:
                $ref: "#/components/schemas/MyObject"
              minItems: 1
    MyObject:
      oneOf:
        - $ref: "#/components/schemas/Object1"
        - $ref: "#/components/schemas/Object2"
      x-oaiExpandable: true
    Object1:
      type: object
      properties:
        ...
    Object2:
      type: object
      properties:
        ...

Will generate this invalid Java code

    public List<MyObject> getList<MyObject>() throws ClassCastException {
        return (List<MyObject>)super.getActualInstance();
    }