-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[kotlin-server] Add polymorphism, oneOf and allOf support #22610
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
Open
dennisameling
wants to merge
7
commits into
OpenAPITools:master
Choose a base branch
from
dennisameling:kotlin-server-polymorphism-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,830
−10
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6b796e2
[kotlin-server] Add polymorphism support
dennisameling d35c664
Fix CI triggers
dennisameling 2ed6dc3
Fix FILES
dennisameling efe90e3
Fix samples and related triggers
dennisameling a4777ba
Fix FILES
dennisameling 3abe602
Add discriminator property to sealed class
dennisameling 6df9076
Fix double nullability issue
dennisameling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
bin/configs/kotlin-server-polymorphism-allof-and-discriminator.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| generatorName: kotlin-server | ||
| outputDir: samples/server/others/kotlin-server/polymorphism-allof-and-discriminator | ||
| inputSpec: modules/openapi-generator/src/test/resources/3_1/polymorphism-allof-and-discriminator.yaml | ||
| templateDir: modules/openapi-generator/src/main/resources/kotlin-server | ||
| additionalProperties: | ||
| artifactId: kotlin-server-polymorphism-allof-and-discriminator | ||
| library: javalin6 |
10 changes: 10 additions & 0 deletions
10
bin/configs/kotlin-server-polymorphism-and-discriminator-disabled-jackson-fix.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| generatorName: kotlin-server | ||
| outputDir: samples/server/others/kotlin-server/polymorphism-and-discriminator-disabled-jackson-fix | ||
| inputSpec: modules/openapi-generator/src/test/resources/3_1/polymorphism-and-discriminator.yaml | ||
| templateDir: modules/openapi-generator/src/main/resources/kotlin-server | ||
| additionalProperties: | ||
| artifactId: kotlin-server-polymorphism-and-discriminator-disabled-jackson-fix | ||
| library: javalin6 | ||
| # This is set to "true" by default, but we also want to test the case where it's set to false. | ||
| # See KotlinServerCodegen.java for more details about this property. | ||
| fixJacksonJsonTypeInfoInheritance: false |
8 changes: 8 additions & 0 deletions
8
bin/configs/kotlin-server-polymorphism-and-discriminator.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| generatorName: kotlin-server | ||
| outputDir: samples/server/others/kotlin-server/polymorphism-and-discriminator | ||
| inputSpec: modules/openapi-generator/src/test/resources/3_1/polymorphism-and-discriminator.yaml | ||
| templateDir: modules/openapi-generator/src/main/resources/kotlin-server | ||
| additionalProperties: | ||
| artifactId: kotlin-server-polymorphism-and-discriminator | ||
| library: javalin6 | ||
| fixJacksonJsonTypeInfoInheritance: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| generatorName: kotlin-server | ||
| outputDir: samples/server/others/kotlin-server/polymorphism | ||
| inputSpec: modules/openapi-generator/src/test/resources/3_1/polymorphism.yaml | ||
| templateDir: modules/openapi-generator/src/main/resources/kotlin-server | ||
| additionalProperties: | ||
| artifactId: kotlin-server-polymorphism | ||
| library: javalin6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where things get tricky. Let's take this schema as an example:
Pet schema
What the spec says about discriminator and petType
The discriminator is only a hint and does not change validation
In section 4.24.8.8, the spec explicitly says:
This means discriminator by itself doesn’t automatically “require” petType or enforce constraints.
Does the discriminator property have to be present in the payload?
Yes, if the parent schema requires it
In our
Petschema:So the schema rules say: a valid
Petinstance must containpetType, even if the discriminator is "just a hint." The required constraint is what makes it mandatory.Also, in the discriminator examples section the spec states:
So: yes,
petTypemust be present in the payload, because the parent schema requires it and the discriminator usage assumes it exists.openapi-generatorbehavior - with and without the Jackson fix appliedWithout the fix, the generated code looks like this:
Without the fix
As you can see:
🧐
petTypeis not a property of the parent/sealedPetclass. While this works, Jackson does requirepetTypein the JSON payload for being able to deserialize, so we might as well add it as a property to the sealed class for convenience. Note that we needvisible = truein theJsonTypeInfoannotation for Jackson to populate this value.🧐 the
petTypeproperty being present in the children classes (Dog and Cat) now fully depends on whether they're defined in the YAML spec. In this case they are, but it's optional inCatand required inDog. And it's nullable in both cases. While the code generator correctly generated code according to spec here, its practical value is highly questionable, because Jackson requirespetTypeto always be provided in order to be able to properly deserialize.With the fix applied, it looks like this:
With the fix applied
As you can see:
✅
petTypeis now a required, non-nullable property on the parent/sealedPetclass. Since Jackson already requires it to be set forJsonTypeInfoto be able to deserialize things properly, andvisible = true, we can be assured that the property will always be set by Jackson.✅ The
petTypeproperty is now present as a required, non-nullable property in the children classes (Dog and Cat). As mentioned above, Jackson already requires this in order to be able to deserialize properly.This might not be desired behavior in all cases
Let's say I want to re-use the
Catclass elsewhere without using the discriminator, it might not be expected thatpetTypeis now a required, non-nullable field on the Kotlin data class, because the spec says that it's not required for theCatmodel. It's probably not the best example, but I hope you get my train of thought.For this reason, I wanted to make this behavior configurable. But to be honest, it feels like a bit of an edge case, and we might as well not make it configurable (always applying this fix for Jackson). Curious what y'all think about this.