Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #5382
Closed
Open
Issue created Feb 20, 2020 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

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

Created by: 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 acf85921 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 acf85921. (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

Assignee
Assign to
Time tracking