Skip to content

[kotlin][moshi] add polymorphic support with sealed classes #9193

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
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kvn-stgl
Copy link
Contributor

@kvn-stgl kvn-stgl commented Apr 6, 2021

This is almost the same PR as #9159 but it also adds support for sealed classes together with moshi. It will also resolve the issue #8059. I created a separate PR because the changes of my old PR are mostly in the templates (and straight forward). I think this one is a bit more complex, so it make sense to merge the other one first.

Currently it's not possible to use Kotlin and Moshi with the oneOf-Keyword. I tried to implement the PolymorphicJsonAdapterFactory to support this feature.
The reason why I set the target branch to master is because the current strategy will fail during runtime (it's not allowed with moshi to serialize a interface directly).

I tested it with the following config:

generatorName: kotlin
outputDir: samples/client/3_0/kotlin-retrofit2-moshi-polymorphic
library: jvm-retrofit2
inputSpec: modules/openapi-generator/src/test/resources/3_0/allOf.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
  serializationLibrary: moshi
  artifactId: kotlin-retrofit2-moshi-polymorphic
  moshiCodeGen: true
  useSealedClass: true

Saddly in the current petstore samples are no discriminator, that's why I didn't add a sample.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master, 5.1.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

technical committee : @jimschubert @dr4ke616 @karismann @Zomzog @andrewemery @4brunu @yutaka0m

@auto-labeler
Copy link

auto-labeler bot commented Apr 6, 2021

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@rhys-rant
Copy link

Is there any scope for progressing with these changes? We're hitting up against a problem with generating an API client from a spec that uses a discriminator and oneOf for representing two discrete cases, but the code we're being generated merges the properties instead.
Seems like exactly what sealed classes would solve, especially given how the Swift generator generates an enum with associated cases for each type in the oneOf.
It'd be a massive help if we could have access in any way to generating sealed classes instead of the generator merging types!

@rhys-rant
Copy link

Is there any chance of getting this functionality merged?

@wing328
Copy link
Member

wing328 commented Aug 8, 2023

@rhys-rant do you mind filing a new PR based on this one with resolved merge conflicts?

@mattiasback-ps
Copy link

mattiasback-ps commented Nov 23, 2023

I also would like to see this merged asap.

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

Successfully merging this pull request may close these issues.

4 participants