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

OpenAPI Generator generates classes that can't build for models with two (or more) levels of inheritance and a discriminator #1206

Closed
runenielsen opened this issue Sep 12, 2023 · 2 comments · Fixed by #1214

Comments

@runenielsen
Copy link

Expected Behavior

When the OpenAPI Generator generates classes for a OpenAPI definition with a model with two (or more) levels of inheritance and using a discriminator, the generated classes should build, and the discriminator field should not be part of the constructor parameters in any of the generated classes.

Actual Behaviour

The sub-class (BasicBookInfo in my example below) is generated correctly, but the sub-sub-class (DetailedBookInfo in my example below) has the discriminator field as part of the constructor parameters and calls it's parent's (BasicBookInfo) constructor with incorrect parameters.

This means the generated classes can't build.

I think the problem is related to the fix done in this issue, since in version 4.0.3 of the OpenAPI plugin, the discriminator was part of all constructors, and the generated classes could build even with two levels of inheritance.

I think it is preferable, if the discriminator is not part of the constructor of any generated classes, as it is not required, and there's actually a bug in Micronaut Serialization, that causes the discriminator field to be output twice in the JSON when serializing, if it is set in the model.

But if it is not possible to both get this and classes that build, I suppose classes that build are preferable 🙂

Steps To Reproduce

  1. Clone the project https://github.com/runenielsen/discriminatorconstructorbug
  2. Try to build.
  3. See that it fails and inspect the constructor in the generated class DetailedBookInfo. The parameter name is missing in the call to the super class. Also TypeEnum type should not be a parameter in DetailedBookInfo's constructor signature.
  4. Try and downgrade the OpenAPI plugin to 4.0.3 in build.gradle.kts (id("io.micronaut.openapi") version "4.0.3") and see that now it builds.
  5. To get the tests in the project to run with 4.0.3, you need to change all occurrences of "DETAILED" to "DetailedBookInfo" in SerdeWithDiscriminatorTest due to this unrelated bug, that was fixed in version 4.1.0.

Environment Information

OS: MacOS
JDK: 17

Example Application

https://github.com/runenielsen/discriminatorconstructorbug

Version

4.1.0

@altro3
Copy link
Collaborator

altro3 commented Sep 13, 2023

Ok, I'll fix it on the weekend

@runenielsen
Copy link
Author

runenielsen commented Sep 13, 2023

@altro3 : Thanks a lot. I also just created another issue, that is similar: #1208 , so it may be easier to fix both at the same time.

This issue (no. 1206) is more important to fix though, since I think that it is functionality used more often.

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

Successfully merging a pull request may close this issue.

2 participants