Modern software development is for a large part integrating with other software. To integrate with other software you use the API software you want to integrate with. An API can be a Java-interface, a function, a programming language, DSL, Graphql endpoint, SOAP-Webservice or OpenAPIt. Not to wander off, in this post I describe a few steps I had to take to use existing OpenAPI in Scala.
As described in the previous post, I managed to create a openapi document describing the Woocommerce api.
Generating a client based on Openapi-document
A openapi yml document can be used to generate client code using the openapi-generator . After installing, the command below, using a few additional properties, would generate a scala client using the akka library in the directory out:
openapi-generator-cli generate -g scala-akka -o woocommerce-akka-client -i src/main/resources/woocommerce-openapi.yml --additional-properties=mainPackage=org.woocommerce.akkaclient,artifactId=woocommerce-akka-client,groupId=org.woocommerce
Shaving a yak, sbt-openapi-generator
Now it would be nice to integrate the generation of the client into the sbt. I found a sbt-openapi-generator which looks easy. Unfortunately it seems the project isn’t very active. Old libraries can still be useful, but for Scala 2.x unmaintained projects are a bit problematic: it’s compiled for Scala 2.11 and 2.12 and doesn’t work for Scala 2.13 yet. I tried to get the plugin to work for 2.13 but get some weird error (
Error downloading org.scala-sbt:scripted-sbt_2.13:1.3.10 ), and apparently someone’s already reported that issue. Not going to shave this yak any further, I’ll go back to npm cli.
Custom sbt task
As the sbt-openapi-generator doesn’t work yet, I’ll default to a custom task.
In the directory project I’ve added a file OpenApiGenerator which just executes the command and also does some search and replace so the scala version in the build file matches those of the parent project.
I’ve updated my build.sbt project to include the following, so the command is available:
lazy val root = (project in file(".")) .settings( name := "woocommerce-scala-client", libraryDependencies += scalaTest % Test, commands ++= Seq(generateAkkaClient) ) .aggregate(akkaClient)
I’ve published the resulting code, as well as the above generator in Github repository:
To use, checkout the project and run sbt publishLocal. I’ll may try to have the artifact updated as well.
One Reply to “Using OpenAPI in Scala”
[…] least has a proper api documentation, and with some effort I managed to create an openapi-document. Next posting, I will try to generate a client based on the […]