Skip to content

[BUG] OneOf inside property of a schema generates broken Java Client #5382

@rkoehn

Description

@rkoehn

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • 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 acf8592 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 acf8592. (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

#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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions