How do I swagger generate Kotlin?
Asked Answered
C

2

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 [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
Corrade answered 16/3, 2018 at 19:40 Comment(2)
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+?Grory
I'm sure of the Swagger Codegen versions. I will look at other ways to install it that may eliminate this error.Corrade
C
7

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

mvn clean package

made kotlin appear as a recognized switch.

Corrade answered 16/3, 2018 at 20:26 Comment(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). github.com/looker-open-source/sdk-codegen is the repository.Corrade
P
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.

Pyrolysis answered 5/7, 2021 at 14:28 Comment(6)
Thanks. At the time I asked this, openapi-generator didn't exist. I now use Looker's code generator 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.Corrade
@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 openapiStatampere
@Pyrolysis 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.Statampere
If you have a gradle project and you need to generate the classes, you can use the openapi 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).Pyrolysis
@Statampere 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 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. I make no warranties for others.Corrade
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.Corrade

© 2022 - 2024 — McMap. All rights reserved.