Skip to content

[kotlin][moshi] add polymorphic support #9159

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 3 commits into
base: master
Choose a base branch
from

Conversation

kvn-stgl
Copy link
Contributor

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

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"

Saddly in the current petstore samples are no discriminator, that's why I didn't add a sample. My plans for the future are to directly change the interface to a sealed class. Any recommendations for the strategy therefore?

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 1, 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.

@kvn-stgl
Copy link
Contributor Author

kvn-stgl commented Apr 4, 2021

Short note: This is also a first step to support #8059 together with Moshi.

@kvn-stgl kvn-stgl changed the title [Kotlin][Moshi] Add polymorphic support [kotlin][moshi] add polymorphic support Apr 6, 2021
@wing328 wing328 added this to the 5.1.1 milestone Apr 16, 2021
{{#moshi}}
@Json(name = "{{{vendorExtensions.x-base-name-literal}}}")
{{/moshi}}
{{#moshi}}{{^discriminator}}@Json(name = "{{{vendorExtensions.x-base-name-literal}}}"){{/discriminator}}{{/moshi}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

     {{#moshi}}
     {{^discriminator}}@Json(name = "{{{vendorExtensions.x-base-name-literal}}}"){{/discriminator}}
     {{/moshi}}

to avoid blank line in the output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed. Thanks for the hint 👍

@@ -51,6 +52,8 @@ import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
{{/kotlinx_serialization}}

{{#moshi}}import {{modelPackage}}.*{{/moshi}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed. Thanks for the hint 👍

@wing328
Copy link
Member

wing328 commented Apr 16, 2021

My plans for the future are to directly change the interface to a sealed class. Any recommendations for the strategy therefore?

Please test with https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml#L1927 instead.

For oneOf implementation in Kotline, the oneOf impelmentation in java (jersey2), csharp-netcore are good starting points. PM me if Slack if you need help on this.

@kvn-stgl kvn-stgl force-pushed the feat/moshi-serialization-polymorphic branch from a811156 to 498086c Compare April 29, 2021 19:10
@wing328 wing328 modified the milestones: 5.1.1, 5.2.0 May 10, 2021
@4brunu
Copy link
Contributor

4brunu commented Jun 1, 2021

@kvn-stgl adding support for oneOf is great, thanks for creating this PR 👍
Could you please fix the merge conflicts and create a sample with a oneOf example?
Thanks

@wing328 wing328 modified the milestones: 5.2.0, 5.2.1 Jul 13, 2021
@wing328 wing328 modified the milestones: 5.2.1, 5.3.0 Aug 17, 2021
@wing328 wing328 modified the milestones: 5.3.0, 5.3.1 Oct 25, 2021
@wing328 wing328 modified the milestones: 5.3.1, 5.4.0 Dec 29, 2021
@wing328 wing328 modified the milestones: 5.4.0, 6.0.0 Jan 31, 2022
@kuFEAR
Copy link
Contributor

kuFEAR commented Apr 7, 2022

Any updates? And I want to adopt the same logic for kotlinx.serialization

@4brunu
Copy link
Contributor

4brunu commented Apr 7, 2022

Not that I'm aware of

@kuFEAR
Copy link
Contributor

kuFEAR commented Apr 13, 2022

where I can look on reference implementation of polymorphic codegen? As I see in the PR author don't add template with inheritance of sealed class in subclasses

@kuFEAR
Copy link
Contributor

kuFEAR commented Apr 14, 2022

@wing328 wing328 modified the milestones: 6.0.0, 6.1.0, 6.0.1 May 26, 2022
@wing328 wing328 modified the milestones: 6.0.1, 6.1.0 Jul 5, 2022
@cwahl1
Copy link

cwahl1 commented Jul 27, 2022

@4brunu I'm going start working on resolving the issues in this PR. Where should I submit my work when I'm done?

@4brunu
Copy link
Contributor

4brunu commented Jul 27, 2022

You should fork this repo, make the changes in your fork, and then create a PR back to this repo.

@wing328 wing328 modified the milestones: 6.1.0, 6.1.1 Sep 11, 2022
@wing328 wing328 modified the milestones: 6.1.1, 6.2.1 Sep 24, 2022
@wing328 wing328 modified the milestones: 6.2.1, 6.3.0 Nov 1, 2022
@wing328 wing328 modified the milestones: 6.3.0, 6.3.1 Jan 20, 2023
@wing328 wing328 modified the milestones: 6.4.0, 6.5.0 Feb 19, 2023
@wing328 wing328 modified the milestones: 6.5.0, 6.6.0 Apr 1, 2023
@wing328 wing328 modified the milestones: 6.6.0, 7.0.0 May 11, 2023
@wing328 wing328 removed this from the 7.0.0 milestone Aug 11, 2023
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.

5 participants