7

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 git@github.com: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
stkent
  • 19,772
  • 14
  • 85
  • 111
John Kaster
  • 2,509
  • 1
  • 33
  • 40
  • The language id is `kotlin`, it's a client generator. How did you install the Swagger Codegen? Are you sure you are using Codegen v. 2.2.3+? – Helen Mar 16 '18 at 19:46
  • I'm sure of the Swagger Codegen versions. I will look at other ways to install it that may eliminate this error. – John Kaster Mar 16 '18 at 20:19

2 Answers2

7

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

mvn clean package

made kotlin appear as a recognized switch.

John Kaster
  • 2,509
  • 1
  • 33
  • 40
  • 1
    FWIW, I've been developing a new SDK code generator on behalf of my employer that generates Kotlin and a couple other languages (so far). https://github.com/looker-open-source/sdk-codegen is the repository. – John Kaster Nov 10 '19 at 01:13
1

You can also use openapi generator to generate the code in kotlin using:

openapi-generator generate -i petstore.yml -g kotlin  --config api-config.json

openapi generator is a fork of the swagger codegen and offers the same kind of functionalities. Find the kotlin generators specs in the documentation.

Sylhare
  • 5,907
  • 8
  • 64
  • 80
  • 2
    Thanks. At the time I asked this, openapi-generator didn't exist. I now use Looker's [code generator](https://github.com/looker-open-source/sdk-codegen) instead that works with both Swagger and OpenAPI specifications (I'm primary author on it, currently). Disclaimer: it works on our specs. Untested with others. – John Kaster Jul 13 '21 at 00:35
  • @JohnKaster how is Looker's solution better than swagger's and aren't swagger and openAPI the same? I'm looking for a codegen for an Android/Kotlin proyect and v3 openapi – David Oct 27 '21 at 21:49
  • @Sylhare I'm still not able to use the generated code, there is so much files being generated that I'm so confused, there is a manifest, settings.gradle, etc. soooo many files, I suppose most of the people only expect the classes, I'm really having a hard time trying to figure out how to use the generated files, which ones to keep, where to put them, etc. – David Oct 28 '21 at 00:41
  • 1
    If you have a gradle project and you need to generate the classes, you can use the [openapi gradle plugin](https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-gradle-plugin) only to generate the classes you need. Here it would be more to generate an entire kotlin project from a swagger/openapi file (hence the gradle files and all). – Sylhare Oct 29 '21 at 13:29
  • @David yes, OpenAPI 3x is a rename from Swagger 3.x. The solution our team has built is definitely better for our purposes, but your needs may not be the same. In anticipation of this question, a few years ago I wrote this [rationale](https://github.com/looker-open-source/sdk-codegen/blob/main/docs/rationale.md) doc to explain our position. With our generator design (we'd like to iterate cleaner) a new language SDK can be produced in < 3 days as explained in [BYO SDK](https://github.com/looker-open-source/sdk-codegen/blob/main/docs/byosdk.md). I make no warranties for others. – John Kaster Nov 03 '21 at 22:12
  • One thing we haven't yet done is full spec-based authentication, although we do have an authentication interface to support alternative API/SDK auth flows. – John Kaster Nov 03 '21 at 22:13