Skip to content

Commit

Permalink
correct circe encoder for OpenAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarwacki committed Feb 10, 2021
1 parent 856570a commit 7f536d3
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ trait TapirOpenAPICirceEncoders {
implicit val encoderInfo: Encoder[Info] = deriveEncoder[Info]
implicit val encoderContact: Encoder[Contact] = deriveEncoder[Contact]
implicit val encoderLicense: Encoder[License] = deriveEncoder[License]
implicit val encoderOpenAPI: Encoder[OpenAPI] = deriveEncoder[OpenAPI]
implicit val encoderOpenAPI: Encoder[OpenAPI] = deriveEncoder[OpenAPI].mapJson(_.deepDropNullValues)
implicit val encoderDiscriminator: Encoder[Discriminator] = deriveEncoder[Discriminator]
implicit def encodeList[T: Encoder]: Encoder[List[T]] = {
case Nil => Json.Null
Expand Down
4 changes: 4 additions & 0 deletions doc/docs/openapi.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Generating OpenAPI documentation

To use, add the following dependencies:
Expand Down Expand Up @@ -53,9 +54,12 @@ OpenAPIDocsInterpreter.toOpenAPI(List(addBook, booksListing, booksListingByGenre
The openapi case classes can then be serialised, either to JSON or YAML using [Circe](https://circe.github.io/circe/):

```scala mdoc:silent
import io.circe.syntax.EncoderOps
import sttp.tapir.openapi.circe._
import sttp.tapir.openapi.circe.yaml._

println(docs.toYaml)
println(docs.asJson)
```

## Options
Expand Down
54 changes: 54 additions & 0 deletions docs/openapi-docs/src/test/resources/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"openapi" : "3.0.3",
"info" : {
"title" : "Fruits",
"version" : "1.0"
},
"paths" : {
"/" : {
"get" : {
"operationId" : "getRoot",
"parameters" : [
{
"name" : "fruit",
"in" : "query",
"required" : true,
"schema" : {
"type" : "string"
}
},
{
"name" : "amount",
"in" : "query",
"required" : false,
"schema" : {
"type" : "integer"
}
}
],
"responses" : {
"200" : {
"description" : "",
"content" : {
"text/plain" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
},
"/api/delete" : {
"delete" : {
"operationId" : "deleteApiDelete",
"responses" : {
"200" : {
"description" : "ok"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sttp.tapir.docs.openapi

import io.circe.syntax.EncoderOps
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import sttp.tapir.openapi._
import sttp.tapir.openapi.circe._
import sttp.tapir.tests._

class VerifyJsonTest extends AnyFunSuite with Matchers {

test("should match the expected json") {
val expectedJson = load("expected.json")

val actualJson = OpenAPIDocsInterpreter.toOpenAPI(List(in_query_query_out_string, delete_endpoint), Info("Fruits", "1.0")).asJson
val actualJsonNoIndent = noIndentation(actualJson.toString())

actualJsonNoIndent shouldBe expectedJson
}
}
Loading

0 comments on commit 7f536d3

Please sign in to comment.