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
20.07k stars 6.16k forks source link

[BUG] [Java] oneOf inception provides wrong output in toJson #18535

Open lollol155 opened 2 weeks ago

lollol155 commented 2 weeks ago
Description

to json functions gives the wrong output

openapi-generator version

7.5.0 -> no library defined

OpenAPI declaration file content or url

The following Yaml


    pt:
      type: object
      additionalProperties:
        nullable: true
        oneOf:
          - $ref: "#/components/schemas/mv"
          - $ref: "#/components/schemas/pi"
          - type: array
            items:
              oneOf:
                - $ref: "#/components/schemas/mv"
                - $ref: "#/components/schemas/pi"
    mv:
      oneOf:
        - $ref: "#/components/schemas/sv"
        - type: array
          items:
            $ref: "#/components/schemas/sv"

    sv:
      oneOf:
        - type: number
          format: double
        - type: string

    pi:
      type: object
      properties:
        value:
          $ref: "#/components/schemas/mv"
      additionalProperties:
        $ref: "#/components/schemas/pt"

With the following Java


        Pi pi = new Pi().value(new Mv(new Sv(1d)));
        pi.putAdditionalProperty("key", new Pi().value(new Mv(new Sv(0d))));

        pi.toJson();
        log.debug("{}", pi.toJson());

gives:

{"value":{"instance":1.0,"isNullable":false,"schemaType":"oneOf"},"key":{"value":{"instance":0.0,"isNullable":false,"schemaType":"oneOf"}}}

Where i would expect

{"value": 1.0,"key":{"value": 0.0}}
wing328 commented 2 weeks ago

please give it a try with the latest master as we recently merged a fix

snapshot version can be found in project's readme

lollol155 commented 2 weeks ago

Same output on 7.6.0-SNAPSHOT :(

Many scenarios with oneOf and Arrays work, but this specific scenario fails for me. But i managed to make a smaller example. It happens already with a oneOf in a oneOf

    myModel:
      type: object
      properties:
        value:
          oneOf:
            - oneOf:
                - type: number
                  format: double
                - type: number
                  format: float
            - type: string
log.debug("{}", new MyModel().value(new MyModelValue(new MyModelValueOneOf(10d)).toJson()));
wing328 commented 2 weeks ago

looks like such use case is not yet supported at the moment.

would you have time to contribute a PR to support this use case? I can show you some good starting points.

lollol155 commented 2 weeks ago

That would be fun to give a try.