Skip to content

Commit

Permalink
OpenAPI tests now compare json ASTs, to avoid string render differences
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Oct 22, 2023
1 parent 63de2ea commit 426177b
Showing 1 changed file with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zio.http.endpoint.openapi

import zio.Scope
import zio.json.DecoderOps
import zio.json.ast.Json
import zio.test._

Expand Down Expand Up @@ -53,13 +54,17 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
.out[SimpleOutputBody]
.outError[NotFoundError](Status.NotFound)

def minify(str: String): String =
Json.encoder.encodeJson(Json.decoder.decodeJson(str).toOption.get, None).toString
def toJsonAst(str: String): Json =
Json.decoder.decodeJson(str).toOption.get

def toJsonAst(api: OpenAPI): Json =
toJsonAst(api.toJson)

override def spec: Spec[TestEnvironment with Scope, Any] =
suite("OpenAPIGenSpec")(
test("simple endpoint to OpenAPI") {
val generated = OpenAPIGen.fromEndpoints("Simple Endpoint", "1.0", simpleEndpoint)
val json = generated.toJson
val json = toJsonAst(generated)
val expectedJson = """{
| "openapi" : "3.1.0",
| "info" : {
Expand Down Expand Up @@ -222,11 +227,11 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| }
| }
|}""".stripMargin
assertTrue(json == minify(expectedJson))
assertTrue(json == toJsonAst(expectedJson))
},
test("with query parameter") {
val generated = OpenAPIGen.fromEndpoints("Simple Endpoint", "1.0", queryParamEndpoint)
val json = generated.toJson
val json = toJsonAst(generated)
val expectedJson = """{
| "openapi" : "3.1.0",
| "info" : {
Expand Down Expand Up @@ -350,11 +355,11 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| }
| }
|}""".stripMargin
assertTrue(json == minify(expectedJson))
assertTrue(json == toJsonAst(expectedJson))
},
test("alternative input") {
val generated = OpenAPIGen.fromEndpoints("Simple Endpoint", "1.0", alternativeInputEndpoint)
val json = generated.toJson
val json = toJsonAst(generated)
val expectedJson =
"""{
| "openapi" : "3.1.0",
Expand Down Expand Up @@ -497,7 +502,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| }
| }
|}""".stripMargin
assertTrue(json == minify(expectedJson))
assertTrue(json == toJsonAst(expectedJson))
},
test("alternative output") {
val endpoint =
Expand All @@ -508,7 +513,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
.content[NotFoundError] ?? Doc.p("not found")) ?? Doc.p("alternative outputs"),
)
val generated = OpenAPIGen.fromEndpoints("Simple Endpoint", "1.0", endpoint)
val json = generated.toJson
val json = toJsonAst(generated)
val expectedJson =
"""{
| "openapi" : "3.1.0",
Expand Down Expand Up @@ -619,7 +624,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| }
| }
|}""".stripMargin
assertTrue(json == minify(expectedJson))
assertTrue(json == toJsonAst(expectedJson))
},
test("with examples") {
val endpoint =
Expand All @@ -639,7 +644,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
)

val generated = OpenAPIGen.fromEndpoints("Simple Endpoint", "1.0", endpoint)
val json = generated.toJson
val json = toJsonAst(generated)
val expectedJson =
"""{
| "openapi" : "3.1.0",
Expand Down Expand Up @@ -791,7 +796,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| }
| }
|}""".stripMargin
assertTrue(json == minify(expectedJson))
assertTrue(json == toJsonAst(expectedJson))
},
test("with query parameter, alternative input, alternative output and examples") {
val endpoint =
Expand All @@ -811,7 +816,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
)

val generated = OpenAPIGen.fromEndpoints("Simple Endpoint", "1.0", endpoint)
val json = generated.toJson
val json = toJsonAst(generated)
val expectedJson =
"""{
| "openapi" : "3.1.0",
Expand Down Expand Up @@ -976,7 +981,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| }
| }
|}""".stripMargin
assertTrue(json == minify(expectedJson))
assertTrue(json == toJsonAst(expectedJson))
},
test("multipart") {
val endpoint = Endpoint(GET / "test-form")
Expand All @@ -988,7 +993,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
HttpCodec.content[ImageMetadata]("metadata"),
)
val generated = OpenAPIGen.fromEndpoints("Simple Endpoint", "1.0", endpoint)
val json = generated.toJson
val json = toJsonAst(generated)
val expected = """{
| "openapi" : "3.1.0",
| "info" : {
Expand All @@ -998,58 +1003,58 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| "paths" : {
| "/test-form" : {
| "get" : {
| "requestBody" :
| "requestBody" :
| {
| "content" : {
| "application/json" : {
| "schema" :
| "schema" :
| {
| "type" :
| "type" :
| "null"
| }
| }
| },
| "required" : false
| },
| "responses" : {
| "default" :
| "default" :
| {
| "description" : "",
| "content" : {
| "multipart/form-data" : {
| "schema" :
| "schema" :
| {
| "type" :
| "type" :
| "object",
| "properties" : {
| "image" : {
| "type" :
| "type" :
| "string",
| "contentEncoding" : "binary",
| "contentMediaType" : "application/octet-stream"
| },
| "height" : {
| "type" :
| "type" :
| "integer",
| "format" : "int32"
| },
| "metadata" : {
| "$ref" : "#/components/schemas/ImageMetadata"
| },
| "title" : {
| "type" :
| "type" :
| [
| "string",
| "null"
| ]
| },
| "width" : {
| "type" :
| "type" :
| "integer",
| "format" : "int32"
| }
| },
| "additionalProperties" :
| "additionalProperties" :
| false,
| "required" : [
| "image",
Expand All @@ -1069,22 +1074,22 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| },
| "components" : {
| "schemas" : {
| "ImageMetadata" :
| "ImageMetadata" :
| {
| "type" :
| "type" :
| "object",
| "properties" : {
| "name" : {
| "type" :
| "type" :
| "string"
| },
| "size" : {
| "type" :
| "type" :
| "integer",
| "format" : "int32"
| }
| },
| "additionalProperties" :
| "additionalProperties" :
| true,
| "required" : [
| "name",
Expand All @@ -1094,7 +1099,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| }
| }
|}""".stripMargin
assertTrue(json == minify(expected))
assertTrue(json == toJsonAst(expected))
},
test("multiple endpoint definitions") {
val generated =
Expand All @@ -1105,7 +1110,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
queryParamEndpoint,
alternativeInputEndpoint,
)
val json = generated.toJson
val json = toJsonAst(generated)
val expected =
"""{
| "openapi" : "3.1.0",
Expand Down Expand Up @@ -1407,7 +1412,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| }
| }
|}""".stripMargin
assertTrue(json == minify(expected))
assertTrue(json == toJsonAst(expected))
},
)

Expand Down

0 comments on commit 426177b

Please sign in to comment.