Skip to content

Commit

Permalink
[openapi] fix incomplete handling (fails on Schema.Lazy) for openapi'…
Browse files Browse the repository at this point in the history
…s fromEndpoints (#2935)

* add a failing test

* fix code to pass failed test
  • Loading branch information
hochgi authored Jun 26, 2024
1 parent a98ed32 commit f5c0411
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions zio-http-gen/src/test/scala/zio/http/gen/model/Data.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package zio.http.gen.model

case class Data(name: String, meta: Meta)
object Data {
implicit val codec: zio.schema.Schema[Data] = zio.schema.DeriveSchema.gen[Data]
}
29 changes: 29 additions & 0 deletions zio-http-gen/src/test/scala/zio/http/gen/model/Meta.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package zio.http.gen.model

import zio.schema.annotation.{caseName, discriminatorName}
import zio.schema.{DeriveSchema, Schema}

@discriminatorName("type")
sealed trait Meta
object Meta {

implicit val codec: Schema[Meta] = DeriveSchema.gen[Meta]

@caseName("a")
case class MetaA(
t: String,
i: Int,
) extends Meta
object MetaA {
implicit val codec: Schema[MetaA] = DeriveSchema.gen[MetaA]
}

@caseName("b")
case class MetaB(
t: String,
i: Int,
) extends Meta
object MetaB {
implicit val codec: Schema[MetaB] = DeriveSchema.gen[MetaB]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package zio.http.gen.openapi

import java.nio.file._

import scala.util.Try

import zio._
import zio.test._

import zio.http._
import zio.http.codec.HeaderCodec
import zio.http.codec.HttpCodec.{query, queryInt}
import zio.http.endpoint._
import zio.http.endpoint.openapi.JsonSchema.SchemaStyle.Inline
import zio.http.endpoint.openapi.JsonSchema.SchemaStyle.{Compact, Inline}
import zio.http.endpoint.openapi.{OpenAPI, OpenAPIGen}
import zio.http.gen.model._
import zio.http.gen.scala.Code
Expand Down Expand Up @@ -1074,6 +1076,10 @@ object EndpointGenSpec extends ZIOSpecDefault {

assertTrue(scala.files.head == expected)
},
test("generates case class for response with compact schema") {
val endpoint = Endpoint(Method.POST / "api" / "v1" / "data").out[Chunk[Data]](status = Status.Ok)
assertTrue(OpenAPIGen.fromEndpoints("", "", Compact, endpoint).components.get.schemas.size == 4)
},
test("generates case class with seq field for request") {
val endpoint = Endpoint(Method.POST / "api" / "v1" / "users").in[UserNameArray].out[User]
val openAPI = OpenAPIGen.fromEndpoints("", "", endpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ object JsonSchema {
schema match {
case enumSchema: Schema.Enum[_] => refForTypeId(enumSchema.id, referenceType)
case record: Schema.Record[_] => refForTypeId(record.id, referenceType)
case lazySchema: Schema.Lazy[_] => nominal(lazySchema.schema, referenceType)
case _ => None
}

Expand Down

0 comments on commit f5c0411

Please sign in to comment.