Openapi-generator: [BUG] OneOf inside property of a schema generates broken Java Client

Created on 20 Feb 2020  路  2Comments  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

  • [X] Have you provided a full/minimal spec to reproduce the issue?
  • [X] Have you validated the input using an OpenAPI validator (example)?
  • [X] What's the version of OpenAPI Generator used?
  • [X] Have you search for related issues/PRs?
  • [X] What's the actual output vs expected output?
  • [ ] [Optional] Bounty to sponsor the fix (example)
Description


I tried generating code for a model using oneOf with master version acf85921e4e7de115139dd1d84adab5011cd4db5 today, and ran into the issue that a number of classes OneOfXxxxYyyyy are imported and used in the generated code, but there are no Java files defining these types.

Everything works fine if I use oneOf at the top level of a schema with no additional properties, effectively making this an interface. However, when I build an object with multiple properties, and one of them is a oneOf, then the generated code is broken.

A complete (broken) example and a slighty different version that works is below.

openapi-generator version


This issues happens with version acf85921e4e7de115139dd1d84adab5011cd4db5. (master branch before 4.3.0).

OpenAPI declaration file content or url

A complete (broken) example is here:

openapi: "3.0.1"
info:
  version: "1.0.0"
  title: "oneOf Test"
paths:
  /magic:
    get:
      responses:
        200:
          $ref: '#/components/schemas/Response'
components:
  schemas:
    ResultA:
      type: "object"
      properties:
        type:
          type: "string"
    ResultB:
      type: "object"
      properties:
        type:
          type: "string"
    Response:
      type: "object"
      properties:
        message:
          type: "string"
        result:
          oneOf:
            - $ref: '#/components/schemas/ResultA'
            - $ref: '#/components/schemas/ResultB'
          discriminator:
            propertyName: type
            mapping:
              resultA: '#/components/schemas/ResultA'
              resultB: '#/components/schemas/ResultB'

The error occurring during maven build of the generated models is:

[ERROR] /tmp/javasdk/src/main/java/org/openapitools/client/model/Response.java:[24,37] cannot find symbol
[ERROR]   symbol:   class OneOfResultAResultB
[ERROR]   location: package org.openapitools.client.model
[ERROR] /tmp/javasdk/src/main/java/org/openapitools/client/model/Response.java:[40,11] cannot find symbol
[ERROR]   symbol:   class OneOfResultAResultB
[ERROR]   location: class org.openapitools.client.model.Response
...

If I redefine Response in the following way it works, without generating any additional classes:

    Response:
        oneOf:
          - $ref: '#/components/schemas/ResultA'
          - $ref: '#/components/schemas/ResultB'
        discriminator:
          propertyName: type
          mapping:
            resultA: '#/components/schemas/ResultA'
            resultB: '#/components/schemas/ResultB'
Command line used for generation

generate -i openapi.yaml -g java --library jersey2 -o /tmp/javasdk

Steps to reproduce

Run the above command with the example posted above as input schema, then build the generated Java library.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/5120 implemented the feature to make the working example work at all, the broken case from this bug here seems to be a special case currently not properly handled.

Suggest a fix

TBD

Bug

Most helpful comment

Hey 馃憢 thanks for opening this issue, my shot at fixing it is at https://github.com/OpenAPITools/openapi-generator/pull/5400

All 2 comments

Hey 馃憢 thanks for opening this issue, my shot at fixing it is at https://github.com/OpenAPITools/openapi-generator/pull/5400

I faced the same problem, so I hope the PR https://github.com/OpenAPITools/openapi-generator/pull/5400 will be merged soon.

Was this page helpful?
0 / 5 - 0 ratings