Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 of 6 tasks
adam-sickmiller-collibra opened this issue Aug 24, 2023 · 3 comments

Comments

@adam-sickmiller-collibra
Copy link

adam-sickmiller-collibra commented Aug 24, 2023

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.

@adam-sickmiller-collibra
Copy link
Author

I've just noticed that the problem can be worked around by changing the default serialization library away from okhttp-gson. So one workaround fix, in a maven project, this would look like...

                        <configuration>
                            <inputSpec>${basedir}/src/main/resources/my-spec.yml</inputSpec>
                            <generatorName>java</generatorName>
                            <library>resttemplate</library>
                        </configuration>

@adam-sickmiller-collibra
Copy link
Author

I've raised a PR to solve this:
#16478

@adam-sickmiller-collibra
Copy link
Author

Seems to have been solved with #19614 -- closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant