Omit discriminator property for Kotlin serialization #342
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.
Discriminated oneOf models generated with Kotlin serialization will currently not work because the discriminator property causes a name conflict with Kotlin Serialization's class discriminator:
Sealed class 'subclass' cannot be serialized as base class 'com.example.models.Superclass' because it has property name that conflicts with JSON class discriminator 'type'.
The issue was not discovered earlier because the OpenAPI spec for end-2-end test for Kotlin serialization models did not define the discriminator property on the child schemas. This PR adds the missing
type
property to the test.In order for the generated code to be valid with Kotlin serialization we must omit the property in the models. The solution in this PR is to simply skip adding the property as part of the code generation process.
Skipping properties in the generation process has the side effect that if the discriminator is the only property on the schema we are left with a class with zero properties which must then converted to an object (example). It would be more clean to filter out the property before the generation step, but I am not sure where we would hook in in order to do so.
I am really open to feedback on this approach and to any suggestions for improvements.