Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
},
)

}