-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #879 from tg44/openapi-codegen-b3
Openapi codegen improvements - docs and sbt oprions
- Loading branch information
Showing
23 changed files
with
372 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Generate endpoint definitions from an OpenAPI YAML | ||
|
||
```eval_rst | ||
.. note:: | ||
This is a really early alpha implementation. | ||
``` | ||
|
||
## Installation steps | ||
|
||
Add the sbt plugin to the `project/plugins.sbt`: | ||
|
||
```scala | ||
addSbtPlugin("com.softwaremill.sttp.tapir" % "sbt-openapi-codegen" % "@VERSION@") | ||
``` | ||
|
||
Enable the plugin for your project in the `build.sbt`: | ||
|
||
```scala | ||
enablePlugins(OpenapiCodegenPlugin) | ||
``` | ||
|
||
Add your OpenApi file to the project, and override the `openapiSwaggerFile` setting in the `build.sbt`: | ||
|
||
```scala | ||
openapiSwaggerFile := baseDirectory.value / "swagger.yaml" | ||
``` | ||
|
||
At this point your compile step will try to generate the endpoint definitions | ||
to the `sttp.tapir.generated.TapirGeneratedEndpoints` object, where you can access the | ||
defined case-classes and endpoint definitions. | ||
|
||
## Usage and options | ||
|
||
The generator currently supports these settings, you can override them in the `build.sbt`; | ||
|
||
| setting | default value | description | | ||
|---|---|---| | ||
| openapiSwaggerFile | baseDirectory.value / "swagger.yaml" | The swagger file with the api definitions. | | ||
| openapiPackage | sttp.tapir.generated | The name for the generated package. | | ||
| openapiObject | TapirGeneratedEndpoints | The name for the generated object. | | ||
|
||
The general usage is; | ||
|
||
```scala | ||
import sttp.tapir.generated._ | ||
import sttp.tapir.docs.openapi._ | ||
import sttp.tapir.openapi.circe.yaml._ | ||
|
||
val docs = TapirGeneratedEndpoints.generatedEndpoints.toOpenAPI("My Bookshop", "1.0") | ||
``` | ||
|
||
### Limitations | ||
|
||
Currently, the generated code depends on `"io.circe" %% "circe-generic"`. In the future probably we will make the encoder/decoder json lib configurable (PRs welcome). | ||
|
||
We currently miss a lot of OpenApi features like: | ||
- tags | ||
- enums/ADTs | ||
- missing model types and meta descriptions (like date, minLength) | ||
- file handling | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
sphinx_rtd_theme==0.4.3 | ||
recommonmark==0.5.0 | ||
sphinx==2.0.1 | ||
sphinx-autobuild==0.7.1 | ||
sphinx-autobuild==0.7.1 | ||
sphinx-markdown-tables==0.0.15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
### Developer notes | ||
|
||
#### Testing | ||
``` | ||
sbt | ||
project openapiCodegen2_12 | ||
test | ||
scripted | ||
``` | ||
|
||
#### Local debugging | ||
``` | ||
cd sbt/sbt-openapi-codegen/src/sbt-test/sbt-openapi-codegen/minimal/ | ||
sbt -Dplugin.version=0.1-SNAPSHOT run | ||
cat target/swagger.yaml | ||
cat target/scala-2.12/classes/sttp/tapir/generated/TapirGeneratedEndpoints.scala | ||
``` |
8 changes: 6 additions & 2 deletions
8
sbt/sbt-openapi-codegen/src/main/scala/OpenapiCodegenKeys.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import sbt._ | ||
|
||
trait OpenapiCodegenKeys { | ||
lazy val swaggerFile = settingKey[File]("swagger file with the definitions") | ||
lazy val openapiSwaggerFile = settingKey[File]("The swagger file with the api definitions.") | ||
lazy val openapiPackage = settingKey[String]("The name for the generated package.") | ||
lazy val openapiObject = settingKey[String]("The name for the generated object.") | ||
|
||
lazy val generateTapirDefinitions = taskKey[Unit]("Generates tapir definitions based on the input swagger file") | ||
lazy val generateTapirDefinitions = taskKey[Unit]("The task that generates tapir definitions based on the input swagger file.") | ||
} | ||
|
||
object OpenapiCodegenKeys extends OpenapiCodegenKeys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
sbt/sbt-openapi-codegen/src/sbt-test/sbt-openapi-codegen/caching/build.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
lazy val root = (project in file(".")) | ||
.enablePlugins(OpenapiCodegenPlugin) | ||
.settings( | ||
scalaVersion := "2.12.4", | ||
version := "0.1" | ||
) | ||
|
||
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-json-circe" % "0.17.0-M2" | ||
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-openapi-docs" % "0.17.0-M2" | ||
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-openapi-circe-yaml" % "0.17.0-M2" | ||
|
||
import scala.io.Source | ||
|
||
TaskKey[Unit]("check") := { | ||
val reference = Source.fromFile("swagger.yaml").getLines.mkString("\n") | ||
val out = Source.fromFile("target/swagger.yaml").getLines.mkString("\n") | ||
if (out != reference) { | ||
sys.error("unexpected output:\n" + out + "\n\n" + (out diff reference) + "\n\n" + (reference diff out)) | ||
} | ||
() | ||
} |
1 change: 1 addition & 0 deletions
1
sbt/sbt-openapi-codegen/src/sbt-test/sbt-openapi-codegen/caching/project/build.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.3.13 |
11 changes: 11 additions & 0 deletions
11
sbt/sbt-openapi-codegen/src/sbt-test/sbt-openapi-codegen/caching/project/plugins.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
val pluginVersion = System.getProperty("plugin.version") | ||
if(pluginVersion == null) | ||
throw new RuntimeException("""| | ||
| | ||
|The system property 'plugin.version' is not defined. | ||
|Specify this property using the scriptedLaunchOpts -D. | ||
| | ||
|""".stripMargin) | ||
else addSbtPlugin("com.softwaremill.sttp.tapir" % "sbt-openapi-codegen" % pluginVersion) | ||
} |
32 changes: 32 additions & 0 deletions
32
sbt/sbt-openapi-codegen/src/sbt-test/sbt-openapi-codegen/caching/src/main/scala/Main.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
object Main extends App { | ||
/* | ||
import sttp.tapir._ | ||
import sttp.tapir.json.circe._ | ||
import io.circe.generic.auto._ | ||
type Limit = Int | ||
type AuthToken = String | ||
case class BooksFromYear(genre: String, year: Int) | ||
case class Book(title: String) | ||
val booksListing: Endpoint[(BooksFromYear, Limit, AuthToken), String, List[Book], Any] = | ||
endpoint | ||
.get | ||
.in(("books" / path[String]("genre") / path[Int]("year")).mapTo(BooksFromYear)) | ||
.in(query[Limit]("limit").description("Maximum number of books to retrieve")) | ||
.in(header[AuthToken]("X-Auth-Token")) | ||
.errorOut(stringBody) | ||
.out(jsonBody[List[Book]]) | ||
*/ | ||
import sttp.tapir.generated._ | ||
import sttp.tapir.docs.openapi._ | ||
import sttp.tapir.openapi.circe.yaml._ | ||
|
||
val docs = TapirGeneratedEndpoints.generatedEndpoints.toOpenAPI("My Bookshop", "1.0") | ||
|
||
import java.nio.file.{Paths, Files} | ||
import java.nio.charset.StandardCharsets | ||
|
||
Files.write(Paths.get("target/swagger.yaml"), docs.toYaml.getBytes(StandardCharsets.UTF_8)) | ||
} |
54 changes: 54 additions & 0 deletions
54
sbt/sbt-openapi-codegen/src/sbt-test/sbt-openapi-codegen/caching/swagger.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
openapi: 3.0.1 | ||
info: | ||
title: My Bookshop | ||
version: '1.0' | ||
paths: | ||
/books/{genre}/{year}: | ||
get: | ||
operationId: getBooksGenreYear | ||
parameters: | ||
- name: genre | ||
in: path | ||
required: true | ||
schema: | ||
type: string | ||
- name: year | ||
in: path | ||
required: true | ||
schema: | ||
type: integer | ||
- name: limit | ||
in: query | ||
description: Maximum number of books to retrieve | ||
required: true | ||
schema: | ||
type: integer | ||
- name: X-Auth-Token | ||
in: header | ||
required: true | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: '' | ||
content: | ||
application/json: | ||
schema: | ||
type: array | ||
items: | ||
$ref: '#/components/schemas/Book' | ||
default: | ||
description: '' | ||
content: | ||
text/plain: | ||
schema: | ||
type: string | ||
components: | ||
schemas: | ||
Book: | ||
required: | ||
- title | ||
type: object | ||
properties: | ||
title: | ||
type: string |
6 changes: 6 additions & 0 deletions
6
sbt/sbt-openapi-codegen/src/sbt-test/sbt-openapi-codegen/caching/test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
> clean | ||
> run | ||
> check | ||
$ copy-file target/scala-2.12/src_managed/main/sbt-openapi-codegen/TapirGeneratedEndpoints.scala target/TapirGeneratedEndpoints.scala | ||
> compile | ||
$ newer target/TapirGeneratedEndpoints.scala target/scala-2.12/src_managed/main/sbt-openapi-codegen/TapirGeneratedEndpoints.scala |
27 changes: 27 additions & 0 deletions
27
sbt/sbt-openapi-codegen/src/sbt-test/sbt-openapi-codegen/option-overrides/build.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
lazy val root = (project in file(".")) | ||
.enablePlugins(OpenapiCodegenPlugin) | ||
.settings( | ||
scalaVersion := "2.12.4", | ||
version := "0.1", | ||
openapiPackage := "com.example.generated.apis", | ||
openapiObject := "MyExampleEndpoints", | ||
openapiSwaggerFile := baseDirectory.value / "example_swagger.yaml", | ||
) | ||
|
||
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-json-circe" % "0.17.0-M2" | ||
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-openapi-docs" % "0.17.0-M2" | ||
libraryDependencies += "com.softwaremill.sttp.tapir" %% "tapir-openapi-circe-yaml" % "0.17.0-M2" | ||
|
||
|
||
import scala.io.Source | ||
|
||
|
||
TaskKey[Unit]("check") := { | ||
val reference = Source.fromFile("example_swagger.yaml").getLines.mkString("\n") | ||
val out = Source.fromFile("target/swagger.yaml").getLines.mkString("\n") | ||
if (out != reference) { | ||
sys.error("unexpected output:\n" + out + "\n\n" + (out diff reference) + "\n\n" + (reference diff out)) | ||
} | ||
() | ||
} |
Oops, something went wrong.