Skip to content

Kotlin multiplatform client #3900

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

Merged
merged 10 commits into from
Sep 21, 2019

Conversation

andrewemery
Copy link
Contributor

Support for the generation of Kotlin Multiplatform clients.

Background

The kotlin language defined in the Swagger Codegen project currently generates a Kotlin client that can run on the JVM. Somewhat recently, JetBrains introduced Kotlin Multiplatform, a platform that provides the ability to share code between various platforms: JVM, JS, iOS, Windows, etc. This pull request adds the ability to generate a Kotlin Multiplatform client.

Library

The pull request includes Kotlin Multiplatform generation under the kotlin language and introduces two library options:

  1. jvm: (default) to target the existing Kotlin JVM generation.
  2. multiplatform: to target Kotlin Multiplatform generation.

Limitations

The current pull request only currently adds support for JVM and iOS platforms but support for other platforms should be trivial to add.

At present, date and date-time objects are rendered as strings. No attempt is made to integrate any third-party date/time libraries.

Tests

The current implementation doesn't include any tests beyond those already included as part of the kotlin language implementation. A test project can be found here that generates the petstore sample and includes tests to verify the correctness of the implementation.

Comments welcome.

References

#3899
https://github.com/andrewemery/openapi-generator-kotlin-multiplatform-petstore

Kotlin multiplatform allows Kotlin code to be shared across various target platforms.
This implementation generates Swagger clients for JVM and iOS platforms.
@andrewemery andrewemery force-pushed the kotlin-multiplatform-client branch from eb95cd0 to 44fec6f Compare September 17, 2019 04:38
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
set ags=generate --artifact-id "kotlin-client-petstore-multiplatform" -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -g kotlin --library multiplatform -o samples\client\petstore\kotlin-multiplatform
Copy link
Member

Choose a reason for hiding this comment

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

For -i, please use modules\openapi-generator\src\test\resources\2_0\petstore.yaml instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Updated.

@wing328
Copy link
Member

wing328 commented Sep 18, 2019

Please run the following to update the documentation so as to address the CircleCI failure:

"./bin/utils/export_docs_generators.sh"
"./bin/utils/copy-to-website.sh"
"./bin/utils/export_generators_readme.sh"

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {

// workaround for: https://github.com/swagger-api/swagger-codegen/issues/4258
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need this workaround?

I think the issue has been fixed in OpenAPI Generator

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure. I couldn't find a reference to the issue in OpenAPI Generator (and I couldn't remember in what scenario it was necessary). I'll run through some tests and get back to you.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please. If it's still an issue, we need to track it and get it fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Confirmed fixed. Unnecessary code removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've also included a fix for this issue. Happy to submit as a separate pull request if desired.

#3917

Copy link
Member

Choose a reason for hiding this comment

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

Prefer a separate PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Separate pull request created:
#3928

@wing328
Copy link
Member

wing328 commented Sep 21, 2019

I got the following errors when testing the multiplatform client locally:

➜  kotlin-multiplatform git:(andrewemery-kotlin-multiplatform-client) ✗ gradle build
Starting a Gradle Daemon (subsequent builds will be faster)

> Configure project :
Kotlin Multiplatform Projects are an experimental feature.

Please wait while Kotlin/Native compiler 1.3.50 is being installed.
Download https://download.jetbrains.com/kotlin/native/builds/releases/1.3.50/macos/kotlin-native-macos-1.3.50.tar.gz
Download https://download.jetbrains.com/kotlin/native/builds/releases/1.3.50/macos/kotlin-native-macos-1.3.50.tar.gz finished, took 30 s 697 ms
Unpack Kotlin/Native compiler to /Users/williamcheng/.konan/kotlin-native-macos-1.3.50
Unpack Kotlin/Native compiler to /Users/williamcheng/.konan/kotlin-native-macos-1.3.50 finished, took 28 s 111 ms

> Task :compileKotlinIosArm64 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileKotlinIosArm64'.
> Could not resolve all files for configuration ':iosArm64CompileKlibraries'.
   > Could not resolve org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.1.
     Required by:
         project :
      > Could not resolve org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.1.
         > Could not parse module metadata https://jcenter.bintray.com/org/jetbrains/kotlinx/kotlinx-coroutines-core-native/1.3.1/kotlinx-coroutines-core-native-1.3.1.module
            > Unsupported format version '1.0' specified in module metadata. This version of Gradle supports format version 0.4 only.
   > Could not resolve org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.1.
     Required by:
         project : > io.ktor:ktor-client-ios-iosarm64:1.2.4
         project : > io.ktor:ktor-client-json-iosarm64:1.2.4
         project : > io.ktor:ktor-client-serialization-iosarm64:1.2.4
         project : > io.ktor:ktor-client-ios-iosarm64:1.2.4 > io.ktor:ktor-client-core-native:1.2.4 > io.ktor:ktor-client-core-iosarm64:1.2.4
         project : > io.ktor:ktor-client-ios-iosarm64:1.2.4 > org.jetbrains.kotlinx:kotlinx-coroutines-io-native:0.1.14 > org.jetbrains.kotlinx:kotlinx-coroutines-io-iosarm64:0.1.14
         project : > io.ktor:ktor-client-ios-iosarm64:1.2.4 > io.ktor:ktor-client-core-native:1.2.4 > io.ktor:ktor-client-core-iosarm64:1.2.4 > io.ktor:ktor-http-native:1.2.4 > io.ktor:ktor-http-iosarm64:1.2.4
         project : > io.ktor:ktor-client-ios-iosarm64:1.2.4 > io.ktor:ktor-client-core-native:1.2.4 > io.ktor:ktor-client-core-iosarm64:1.2.4 > io.ktor:ktor-http-cio-native:1.2.4 > io.ktor:ktor-http-cio-iosarm64:1.2.4
         project : > io.ktor:ktor-client-ios-iosarm64:1.2.4 > io.ktor:ktor-client-core-native:1.2.4 > io.ktor:ktor-client-core-iosarm64:1.2.4 > io.ktor:ktor-http-native:1.2.4 > io.ktor:ktor-http-iosarm64:1.2.4 > io.ktor:ktor-utils-native:1.2.4 > io.ktor:ktor-utils-iosarm64:1.2.4
      > Could not resolve org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.1.
         > Could not parse module metadata https://jcenter.bintray.com/org/jetbrains/kotlinx/kotlinx-coroutines-core-native/1.3.1/kotlinx-coroutines-core-native-1.3.1.module
            > Unsupported format version '1.0' specified in module metadata. This version of Gradle supports format version 0.4 only.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2m 11s
1 actionable task: 1 executed

Is it supposed to work on Mac as well?

@andrewemery
Copy link
Contributor Author

I got the following errors when testing the multiplatform client locally:

Try it with the wrapper:

./samples/client/petstore/kotlin-multiplatform/gradlew assemble

Is it supposed to work on Mac as well?

Yes. I've tested on Mac and Windows.

@wing328
Copy link
Member

wing328 commented Sep 21, 2019

Thanks for the tips. It works for me in my machine:

CONST Null type=kotlin.Nothing? value=null

> Task :compileKotlinMetadata
w: /Users/williamcheng/Code/openapi-generator/samples/client/petstore/kotlin-multiplatform/src/commonMain/kotlin/org/openapitools/client/apis/PetApi.kt: (124, 15): Unnecessary safe call on a non-null receiver of type Array<String>
w: /Users/williamcheng/Code/openapi-generator/samples/client/petstore/kotlin-multiplatform/src/commonMain/kotlin/org/openapitools/client/apis/PetApi.kt: (169, 13): Unnecessary safe call on a non-null receiver of type Array<String>
w: /Users/williamcheng/Code/openapi-generator/samples/client/petstore/kotlin-multiplatform/src/commonMain/kotlin/org/openapitools/client/apis/PetApi.kt: (314, 119): This declaration is experimental and its usage should be marked with '@io.ktor.util.KtorExperimentalAPI' or '@UseExperimental(io.ktor.util.KtorExperimentalAPI::class)'
w: /Users/williamcheng/Code/openapi-generator/samples/client/petstore/kotlin-multiplatform/src/commonMain/kotlin/org/openapitools/client/apis/UserApi.kt: (239, 17): Unnecessary safe call on a non-null receiver of type String
w: /Users/williamcheng/Code/openapi-generator/samples/client/petstore/kotlin-multiplatform/src/commonMain/kotlin/org/openapitools/client/apis/UserApi.kt: (241, 17): Unnecessary safe call on a non-null receiver of type String

BUILD SUCCESSFUL in 10m 1s
13 actionable tasks: 13 executed





Copy link
Member

Choose a reason for hiding this comment

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

Too many empty lines. I'll see what I can do to fix these in a separate PR

Copy link
Member

@wing328 wing328 left a comment

Choose a reason for hiding this comment

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

LGTM. Tested locally and the result is good.

@wing328 wing328 merged commit 21e0e0d into OpenAPITools:master Sep 21, 2019
@wing328
Copy link
Member

wing328 commented Sep 27, 2019

@andrewemery
Copy link
Contributor Author

Thanks. Looking forward to community feedback and improvements.

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.

3 participants