Skip to content

[BUG] [JAVA] A parent object with a discriminator referencing an enum causes uncompilable client code #16394

Closed
@adam-sickmiller-collibra

Description

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

Uncompilable java client source code is produced when an object has a property needing discrimination on a property where the allowable values are enumerated as its own children. This type of pattern, without the enumeration, is seen in the specification itself at the end of the discriminator section. When the allowable values of the discriminated property are an enum, the generated code of each subclass contains an uncompilable constructor like this, where petType is an enum and #getSimpleName() returns a String:

  public Lizard() {
      this.petType = this.getClass().getSimpleName();
  }

This is a further refinement of the bug definition reported here, with the difference being the context of the problem only apparent in certain types of parent objects:
#16073

openapi-generator version

7.0.0-beta -- problem also in 6.6.0, and 6.5.0. Did not test before those.

OpenAPI declaration file content or url

Gist

Generation Details

I used the CLI generator and the maven generator with the same result.

Steps to reproduce
  1. Generate Java client with above spec
  2. Note uncompilable code at client/model/Lizard.java and its siblings, it will have a constructor like this, where petType is an enum but getSimpleName() returns String:

The code used is this example from the Open API spec itself with one line added. The line that is added to the example to break compilation is line 35 in the gist below, to wit:enum: [ Cat, Dog, Lizard ]

Related issues/PRs

16073
10441

Suggest a fix

I describe a workaround in the related issue above, though it requires introducing a parent response object or similar. Note that this shows that a discriminator can accept an enum as a discriminated property, so long as the parent object does not contain the discriminator.

10959 <- This open PR appears to propose a fix for the issue in Java
14533 <- This open PR appears to propose a fix for the same issue in C#

I would suggest testing the above changes against the problem and using those as a basis for a fix, or using them explicitly if they do fix them problem.

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