Skip to content

Commit

Permalink
Add test for generating OpenAPI for generic payload (zio#2767)
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Aug 16, 2024
1 parent 0255bd3 commit 974655b
Showing 1 changed file with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import zio.schema.{DeriveSchema, Schema}
import zio.http.Method.{GET, POST}
import zio.http._
import zio.http.codec.PathCodec.string
import zio.http.codec.{ContentCodec, Doc, HttpCodec, QueryCodec}
import zio.http.codec.{ContentCodec, Doc, HttpCodec, HttpContentCodec, QueryCodec}
import zio.http.endpoint._

object OpenAPIGenSpec extends ZIOSpecDefault {
Expand Down Expand Up @@ -147,6 +147,12 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
case class B(i: Int)
}

case class WithGenericPayload[A](a: A)

object WithGenericPayload {
implicit def schema[T: Schema]: Schema[WithGenericPayload[T]] = DeriveSchema.gen
}

private val simpleEndpoint =
Endpoint(
(GET / "static" / int("id") / uuid("uuid") ?? Doc.p("user id") / string("name")) ?? Doc.p("get path"),
Expand Down Expand Up @@ -2631,6 +2637,78 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
val expected = toJsonAst(expectedJson)
assertTrue(json == expected)
},
test("Generic payload") {
// TODO: Currently, the applied types of generics are not saved in the schema correctly
// Once this is fixed, we should generate the ref as `#/components/schemas/WithGenericPayloadSimpleInputBody`
val endpoint = Endpoint(RoutePattern.POST / "generic")
.in[WithGenericPayload[SimpleInputBody]]
val openApi = OpenAPIGen.fromEndpoints(endpoint)
val json = toJsonAst(openApi)
val expectedJson = """{
| "openapi" : "3.1.0",
| "info" : {
| "title" : "",
| "version" : ""
| },
| "paths" : {
| "/generic" : {
| "post" : {
| "requestBody" :
| {
| "content" : {
| "application/json" : {
| "schema" :
| {
| "$ref" : "#/components/schemas/WithGenericPayload"
| }
| }
| },
| "required" : true
| }
| }
| }
| },
| "components" : {
| "schemas" : {
| "SimpleInputBody" :
| {
| "type" :
| "object",
| "properties" : {
| "name" : {
| "type" :
| "string"
| },
| "age" : {
| "type" :
| "integer",
| "format" : "int32"
| }
| },
| "required" : [
| "name",
| "age"
| ]
| },
| "WithGenericPayload" :
| {
| "type" :
| "object",
| "properties" : {
| "a" : {
| "$ref" : "#/components/schemas/SimpleInputBody"
| }
| },
| "required" : [
| "a"
| ]
| }
| }
| }
|}
|""".stripMargin
assertTrue(json == toJsonAst(expectedJson))
},
)

}

0 comments on commit 974655b

Please sign in to comment.