Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [JAVA] oneOf generates incorrect model for primitive types #10450

Closed
5 of 6 tasks
dilook opened this issue Sep 22, 2021 · 7 comments · Fixed by #16834
Closed
5 of 6 tasks

[BUG] [JAVA] oneOf generates incorrect model for primitive types #10450

dilook opened this issue Sep 22, 2021 · 7 comments · Fixed by #16834

Comments

@dilook
Copy link

dilook commented Sep 22, 2021

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Using oneOf will generate a model with incorrect wrong types.

Actual output:
I have three files of models: ClientIp.java, Request,java, Response.java. Request.java has field private ClientIp clientIp = null; with type ClientIp

Expected output:
I have only two files of models: Request,java, Response.java. Request.java has field private String clientIp; with string type

openapi-generator version

openapi-generator-cli 5.2.1
commit : 8403e59
built : 2021-08-16T12:52:51Z
source : https://github.com/openapitools/openapi-generator
docs : https://openapi-generator.tech/

openapi-generator-cli 5.3.0-SNAPSHOT
commit : de95e2a
built : 2021-09-22T15:38:29+04:00
source : https://github.com/openapitools/openapi-generator
docs : https://openapi-generator.tech/

OpenAPI declaration file content or url
openapi: 3.0.3

info:
  title: java codegen BUG
  description: oneOf generates incorrect model for primitive types
  version: 1.0.0

paths:
  /openapi/example/path:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Request'
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Response'

components:
  schemas:
    ClientIp:
      oneOf:
        - type: string
          format: ipv4
        - type: string
          format: ipv6
      nullable: false
      example: '127.0.0.1' # or '2001:0db8:85a3:0000:0000:8a2e:0370:7334' for IPv6

    Response:
      properties:
        result:
          type: string

    Request:
      properties:
        result:
          type: string
        client_ip:
          $ref: "#/components/schemas/ClientIp"
Generation Details

java -jar openapi-generator-cli.jar generate -g java -i oneOfbug.yaml -o .\gen\

Steps to reproduce

Generate a java client using the above yaml.

Related issues/PRs

it little bit similar to #4130

Suggest a fix
@msmerc
Copy link
Contributor

msmerc commented Sep 22, 2021

I see the same with any OneOf, for example:

components:
  schemas:
    IntervalDateRange:
      type: object
      properties:
        foo:
          type: string

    DurationDateRange:
      type: object
      properties:
        foo:
          type: string

    Response:
      properties:
        result:
          type: string

    Request:
      properties:
        result:
          type: string
        client_ip:
          oneOf:
            - $ref: "#/components/schemas/IntervalDateRange"
            - $ref: "#/components/schemas/DurationDateRange"

The compilation error is at import org.openapitools.client.model.OneOfIntervalDateRangeDurationDateRange; - the OneOf... class doesn't appear to have been generated.

If there's a dirty workaround for this I'd love to know!

@wing328
Copy link
Member

wing328 commented Sep 23, 2021

java -jar openapi-generator-cli.jar generate -g java -i oneOfbug.yaml -o .\gen\

Can you please try the jersey2 or native library ?

e.g. add --library jersey2

@msmerc
Copy link
Contributor

msmerc commented Sep 23, 2021

@wing328 I see issues with every library I've tried.

More generally I'm not sure how anyOf oneOf are going to work with primitive / simple types. The current strategy for anyOf seems to be to create an empty java Interface and the have objects implement that interface, but if the objects are final or outside of the scope of the generator (e.g. String, LocalDate) it's never going to work!

I appreciate that it's somewhat bizarre, but the API I have (from Bloomberg no less!) likes to do things like this:

    SnapshotTime:
      type: string
      format: time
    ...
    BvalSnapshotTime:
      allOf:
        - $ref: "#/components/schemas/SnapshotTime"
      description: "The time of the requested BVAL snapshot[...]"

So perhaps the generator could special-case the scenario where anyOf / allOf has a single item?

[More generally what's the prognosis on getting something like this fixed? I'm happy to chip in, and I could even bounty it a little, but I can see you have thousands of open issues so I fear you guys are somewhat swamped!]

@msmerc
Copy link
Contributor

msmerc commented Sep 23, 2021

Simplest workaround I found was to preprocess the yaml to zap out / simplify these cases 😢

@dilook
Copy link
Author

dilook commented Sep 23, 2021

@wing328 I've just researched and known that it's happened for libraries: resttemplate, default okhttp-gson (like in bug).

With native library and jersey2 all is OK. Abstract-class (AbstractOpenApiSchema) is generated with constructor of String type parameter.
So it seems that problem is in generation code for specific libraries.

@ldiaz2-chwy
Copy link

ldiaz2-chwy commented Feb 12, 2022

--library jersey2

ive tried both native and jersey2 and the oneof class is not generated. Is there a workaround?

Using something like so :

method-account:
oneOf:
- $ref: '#/components/schemas/AccountA'
- $ref: '#/components/schemas/AccountB'
- $ref: '#/components/schemas/AccountC'
- $ref: '#/components/schemas/AccountD'
discriminator:
propertyName: account-type

@martin-mfg
Copy link
Contributor

[More generally what's the prognosis on getting something like this fixed? I'm happy to chip in, and I could even bounty it a little, but I can see you have thousands of open issues so I fear you guys are somewhat swamped!]

Hi @msmerc, are you still willing to bounty a solution here?
If so, I think we should clarify what would be the scope of the task (since in this issue, a few different input specs and problems are discussed), and what the bounty would be.
If you have an idea about this, please post it here. We can also discuss this on Slack if you want. This link should take you to my account on Slack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants