Like the official swagger-codegen this project aims to generate Scala source code from Swagger Specification compliant API descriptions.
Moreover, you can do it directly within an sbt
project.
This code generator is designed specifically for Swagger Spec Version 2.0. Moreover, it relies on Play! Framework 2.5 for Json marshalling/unmarshalling, server- and client-side code.
Enable it inside your project\plugins.sbt
like this:
addSbtPlugin("eu.unicredit" % "sbt-swagger-codegen" % "0.0.9")
Enable it in your build.sbt
like this:
enablePlugins(SwaggerCodegenPlugin)
For a super fast hands-on tutorial refer to the related examples and check out sbt-swagger-codegen-examples.
By default, the plugin will assume that you have put your yaml
Swagger specification files under src/main/swagger
.
If so, then you can just run swaggerCodeGen
and it will generate your model's as case classes under src/main/scala/swagger/codegen
and Play Framework Formats for them (for json serialization) under src/main/scala/swagger/codegen/json
.
All available tasks from the plugin
swaggerModelCodeGen
-> generated code for model classesswaggerServerCodeGen
-> generates Play Framework codeswaggerClientCodeGen
-> generates client code usingplay-ws
swaggerRoutesCodeGen
-> generates Play Framework routesswaggerClean
-> cleans up already generated code
The swaggerModelCodeGen
, swaggerServerCodeGen
and swaggerClientCodeGen
will run automatically when the swagger sources change.
swaggerRoutesCodeGen
will not run automatically because it generates code inside the "src/main/resources" directory instead of in "target/scala-2.12/src_managed".
Tasks can be chained with other tasks (ex: (compile in Compile) <<= (compile in Compile) dependsOn swaggerRoutesCodeGen
)
## Keys (and defaults)
These keys influence properties of the generated code itself:
swaggerSourcesDir
-> "/src/main/swagger" (path where to search for swagger files)swaggerCodeGenPackage
-> "swagger.codegen" (package name of the generated sources)swaggerModelFileSplitting
-> "singleFile" (in model generation how to group classes in '.scala' files available options are "oneFilePerSource" "oneFilePerModel")swaggerCodeProvidedPackage
-> "com.yourcompany" (where you will provide business logic server method implementation)
These keys determine where generated files will be put:
swaggerModelCodeTargetDir
-> "target/scala-2.1x/src_managed/src/main/swagger/model" (path where to put generated model files)swaggerGenerateJsonRW
-> true (if you want to generate json Format for your model case classes)swaggerClientCodeTargetDir
-> "target/scala-2.1x/src_managed/src/main/swagger/client" (path where to put generated client code files)swaggerServerCodeTargetDir
-> "target/scala-2.1x/src_managed/src/main/swagger/server" (path where to put generated server code files)swaggerServerRoutesFile
-> "src/main/resources/routes" (routes file to be generated)
These keys can be used to determine what kind of code should be generated:
swaggerGenerateModel
-> true (to be disabled if you do not want model classes to be generated automatically when swagger source code changes)swaggerGenerateClient
-> false (enable this if you want client code to ge generated automatically when swagger source code changes)swaggerGenerateServer
-> false (enable this if you want client code to ge generated automatically when swagger source code changes)
Moreover, you can extend this plugin by providing alternative implementations of the generators via:
swaggerModelCodeGenClass
-> new eu.unicredit.swagger.generators.DefaultModelGenerator() (the class used to generate the model classes)swaggerJsonCodeGenClass
-> new eu.unicredit.swagger.generators.DefaultJsonGenerator() (the class used to generate the json marshaller/unmarshaller)swaggerServerCodeGenClass
-> new eu.unicredit.swagger.generators.DefaultServerGenerator() (the class used to generate the Server classes)swaggerClientCodeGenClass
-> new eu.unicredit.swagger.generators.DefaultClientGenerator() (the class used to generate the client classes)
- scala version 2.11.X
- play-sbt-plugin 2.5.3
- play-json 2.5.3
- play-ws 2.5.3 (only if you use client)
At the moment the project is developed to fullfill some internal projects needs, so do not expect it to cover all the corner cases of the Swagger Spec (i.e. some primitive types in body req or resp). Model objects must have less than 22 parameters and could fail with circular referencies(due to the Play Json macro limitaion).
We are actively working with and on this project, trying to overcome any arising limitations and support all Swagger-spec properties we need. PRs are really welcome and please open an Issue if you find that something is not working.
- Andrea Peruffo: https://github.com/andreaTP
- Francesco Montecuccoli Degli Erri https://github.com/fralken
- Marco Firrincieli: https://github.com/mfirry
*** This is a work in progress and we are not done with it! ***