question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 https://github.com/OpenAPITools/openapi-generator/issues/4130

Suggest a fix

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dilookcommented, 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.

0reactions
ldiaz2-chwycommented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI models not being generated when using oneOf
I have oneOf generation working for java using openapi-generator 5.4.0 but I do add a discriminator field (like requestType: type: string enum: ...
Read more >
Documentation for the spring Generator
Generates a Java SpringBoot Server application using the SpringDoc integration. #CONFIG OPTIONS. These options may be applied as additional- ...
Read more >
JSON Schema Serializer and Deserializer
This is set by specifying json.fail.invalid.schema=true . ... name of a top-level property on the JSON schema that specifies the fully-qualified Java type....
Read more >
C# Generated Code | Protocol Buffers - Google Developers
Compiler Invocation. C#-specific Options · File structure · Messages · Fields. Singular Fields; Repeated Fields; Map fields; Oneof Fields; Wrapper Type Fields.
Read more >
Open API Server Implementation Using OpenAPI Generator
In the modelPackage, a couple of data model POJOs called Error and Pet are generated, based on the schemas defined in our YAML...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found