Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[openapi] fix incomplete handling (fails on Schema.Lazy) for openapi's fromEndpoints #2935

Merged
Show file tree
Hide file tree
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
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
Loading