openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open lollol155 opened this issue 1 year ago • 4 comments

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}}

lollol155 avatar Apr 29 '24 14:04 lollol155

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

snapshot version can be found in project's readme

wing328 avatar Apr 29 '24 17:04 wing328

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()));

lollol155 avatar Apr 29 '24 18:04 lollol155

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.

wing328 avatar Apr 30 '24 05:04 wing328

That would be fun to give a try.

lollol155 avatar Apr 30 '24 14:04 lollol155