How do I swagger generate Kotlin?

John Kaster picture John Kaster · Mar 16, 2018 · Viewed 7.1k times · Source

I'm trying to generate kotlin code from a swagger json file, and I can't figure out the command-line parameters that should be used.

I've tried Swagger codegen v2.3.1 and v2.2.3 and both fail for kotlin, kotlin-client, and kotlin-server with the same error. What's the correct switch to generate Kotlin?

Update from Helen's question: I installed swagger codegen with:

git clone [email protected]:swagger-api/swagger-codegen.git ./swagger-codegen
cd swagger-codegen
git checkout tags/v2.2.3

I also tested with Swagger codegen's bin/kotlin-client-petstore.sh and kotlin was not recognized for that script.

(I've also tried the Swagger Codegen plugin for IntelliJ because it does list kotlin as an output target, but it does not correctly process the swagger json.)

Here's the command-line pattern I'm using, which works for typescript-node:

java -jar ./swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i sample.json -l typescript-node -o typescript_node_sdk

However, it fails for kotlin, kotlin-client, kotlin-server:

$ java -jar ./swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i sample.json -l kotlin -o kotlin_sdk
Exception in thread "main" java.lang.RuntimeException: Can't load config class with name kotlin Available: android
aspnet5
async-scala
csharp
dart
flash
python-flask
go
java
jaxrs
jaxrs-cxf
jaxrs-resteasy
inflector
javascript
javascript-closure-angular
jmeter
nodejs-server
objc
perl
php
python
qt5cpp
ruby
scala
scalatra
silex-PHP
sinatra
slim
spring-mvc
dynamic-html
html
swagger
swagger-yaml
swift
tizen
typescript-angular
typescript-node
akka-scala
CsharpDotNet2
clojure
haskell-servant

at io.swagger.codegen.CodegenConfigLoader.forName(CodegenConfigLoader.java:31)
at io.swagger.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:346)
at io.swagger.codegen.cmd.Generate.run(Generate.java:221)
at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:36)
Caused by: java.lang.ClassNotFoundException: kotlin
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at io.swagger.codegen.CodegenConfigLoader.forName(CodegenConfigLoader.java:29)
... 3 more

Answer

John Kaster picture John Kaster · Mar 16, 2018

Thanks to Helen's question, I looked at Swagger Codegen Building and

mvn clean package

made kotlin appear as a recognized switch.