From f5c04110a885d6c79b640665748eadb802a010d8 Mon Sep 17 00:00:00 2001 From: Gilad Hoch Date: Wed, 26 Jun 2024 19:22:37 +0300 Subject: [PATCH] [openapi] fix incomplete handling (fails on Schema.Lazy) for openapi's fromEndpoints (#2935) * add a failing test * fix code to pass failed test --- .../test/scala/zio/http/gen/model/Data.scala | 6 ++++ .../test/scala/zio/http/gen/model/Meta.scala | 29 +++++++++++++++++++ .../http/gen/openapi/EndpointGenSpec.scala | 8 ++++- .../http/endpoint/openapi/JsonSchema.scala | 1 + 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 zio-http-gen/src/test/scala/zio/http/gen/model/Data.scala create mode 100644 zio-http-gen/src/test/scala/zio/http/gen/model/Meta.scala diff --git a/zio-http-gen/src/test/scala/zio/http/gen/model/Data.scala b/zio-http-gen/src/test/scala/zio/http/gen/model/Data.scala new file mode 100644 index 0000000000..e818d19d4d --- /dev/null +++ b/zio-http-gen/src/test/scala/zio/http/gen/model/Data.scala @@ -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] +} diff --git a/zio-http-gen/src/test/scala/zio/http/gen/model/Meta.scala b/zio-http-gen/src/test/scala/zio/http/gen/model/Meta.scala new file mode 100644 index 0000000000..5963a25b0c --- /dev/null +++ b/zio-http-gen/src/test/scala/zio/http/gen/model/Meta.scala @@ -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] + } +} diff --git a/zio-http-gen/src/test/scala/zio/http/gen/openapi/EndpointGenSpec.scala b/zio-http-gen/src/test/scala/zio/http/gen/openapi/EndpointGenSpec.scala index a30bde7e70..349aebf543 100644 --- a/zio-http-gen/src/test/scala/zio/http/gen/openapi/EndpointGenSpec.scala +++ b/zio-http-gen/src/test/scala/zio/http/gen/openapi/EndpointGenSpec.scala @@ -2,6 +2,8 @@ package zio.http.gen.openapi import java.nio.file._ +import scala.util.Try + import zio._ import zio.test._ @@ -9,7 +11,7 @@ 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 @@ -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) diff --git a/zio-http/shared/src/main/scala/zio/http/endpoint/openapi/JsonSchema.scala b/zio-http/shared/src/main/scala/zio/http/endpoint/openapi/JsonSchema.scala index df2f34d30a..933c52e130 100644 --- a/zio-http/shared/src/main/scala/zio/http/endpoint/openapi/JsonSchema.scala +++ b/zio-http/shared/src/main/scala/zio/http/endpoint/openapi/JsonSchema.scala @@ -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 }