swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.88k stars 6.03k forks source link

[kotlin] Error building kotlin-server with petstore yaml #7591

Open gbak opened 6 years ago

gbak commented 6 years ago
Description

Error generating the kotin-sever code when using the petstore sample yaml file.

Output:

java -jar ./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i ./modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l kotlin-server -o ./petstoreOut

...
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /Users/somedude/projects/swagger/swagger-codegen/./petstoreOut/src/main/kotlin/io/swagger/server/models/User.kt
[main] ERROR io.swagger.codegen.AbstractGenerator - kotlin-server/api.mustache (No such file or directory)
[main] ERROR io.swagger.codegen.AbstractGenerator - can't load template kotlin-server/api.mustache
Exception in thread "main" java.lang.RuntimeException: Could not generate api file for 'Pet'
    at io.swagger.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:542)
    at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:749)
    at io.swagger.codegen.cmd.Generate.run(Generate.java:285)
    at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)
Caused by: java.lang.RuntimeException: can't load template kotlin-server/api.mustache
    at io.swagger.codegen.AbstractGenerator.readTemplate(AbstractGenerator.java:51)
    at io.swagger.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:763)
    at io.swagger.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:501)
    ... 3 more
Swagger-codegen version

master as of Feb 05, 2018

Swagger declaration file content or url
./modules/swagger-codegen/src/test/resources/2_0/petstore.yaml
Command line used for generation
java -jar ./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i ./modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l kotlin-server -o ./petstoreOut
Steps to reproduce

Run the command above against the petstore yaml

gbak commented 6 years ago

With a little modification, I was able to make the generator run:

cd ./modules/swagger-codegen/src/main/resources/kotlin-server
cp libraries/ktor/* .
cd -
mvn package -DskipTests

java -jar ./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i ./modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l kotlin-server -o ./petstoreOut

Also able to build the generated code and test an endpoint.

gradle wrapper
./gradlew assemble
java -jar build/libs/kotlin-server.jar
kmdsbng commented 6 years ago

I got same problem, and gbak's solution fit me. Thank you!

andybarilla commented 6 years ago

By adding --additional-properties library=ktor to the command line, the generation works without any modification.