Skip to content

Commit

Permalink
Fix OpenApi gen components from case class with arity > 22 (#3065)
Browse files Browse the repository at this point in the history
* Fix generate OpenAPI components part from case classes with arity > 22

* Add test case for generating components in OpenAPI gen with case classes with arity > 22

* PR feedback: move test to OpenAPIGenSpec
  • Loading branch information
narma authored Sep 5, 2024
1 parent 1d092b9 commit 8074422
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package zio.http.endpoint.openapi

import zio.schema.{DeriveSchema, Schema}

// case class with 22+ (23 in this case) fields
final case class BigModel(
f1: Boolean,
f2: Boolean,
f3: Boolean,
f4: Boolean,
f5: Boolean,
f6: Boolean,
f7: Boolean,
f8: Boolean,
f9: Boolean,
f10: Boolean,
f11: Boolean,
f12: Boolean,
f13: Boolean,
f14: Boolean,
f15: Boolean,
f16: Boolean,
f17: Boolean,
f18: Boolean,
f19: Boolean,
f20: Boolean,
f21: Boolean,
f22: Boolean,
f23: Boolean,
)
object BigModel {
implicit val codec: Schema[BigModel] = DeriveSchema.gen[BigModel]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3101,6 +3101,142 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
|}""".stripMargin
assertTrue(json == toJsonAst(expectedJson))
},
test("components are generated for big model with 22+ fields") {
val endpoint = Endpoint(Method.GET / "api" / "v1" / "users").out[BigModel]
val generated = OpenAPIGen.fromEndpoints(endpoint)
val json = toJsonAst(generated)
val expectedJson = """{
| "openapi" : "3.1.0",
| "info" : {
| "title" : "",
| "version" : ""
| },
| "paths" : {
| "/api/v1/users" : {
| "get" : {
| "responses" : {
| "200" : {
| "content" : {
| "application/json" : {
| "schema" : {
| "$ref" : "#/components/schemas/BigModel"
| }
| }
| }
| }
| }
| }
| }
| },
| "components" : {
| "schemas" : {
| "BigModel" : {
| "type" : "object",
| "properties" : {
| "f20" : {
| "type" : "boolean"
| },
| "f19" : {
| "type" : "boolean"
| },
| "f7" : {
| "type" : "boolean"
| },
| "f6" : {
| "type" : "boolean"
| },
| "f14" : {
| "type" : "boolean"
| },
| "f18" : {
| "type" : "boolean"
| },
| "f8" : {
| "type" : "boolean"
| },
| "f10" : {
| "type" : "boolean"
| },
| "f5" : {
| "type" : "boolean"
| },
| "f21" : {
| "type" : "boolean"
| },
| "f3" : {
| "type" : "boolean"
| },
| "f9" : {
| "type" : "boolean"
| },
| "f17" : {
| "type" : "boolean"
| },
| "f22" : {
| "type" : "boolean"
| },
| "f15" : {
| "type" : "boolean"
| },
| "f16" : {
| "type" : "boolean"
| },
| "f1" : {
| "type" : "boolean"
| },
| "f13" : {
| "type" : "boolean"
| },
| "f4" : {
| "type" : "boolean"
| },
| "f11" : {
| "type" : "boolean"
| },
| "f23" : {
| "type" : "boolean"
| },
| "f2" : {
| "type" : "boolean"
| },
| "f12" : {
| "type" : "boolean"
| }
| },
| "required" : [
| "f1",
| "f2",
| "f3",
| "f4",
| "f5",
| "f6",
| "f7",
| "f8",
| "f9",
| "f10",
| "f11",
| "f12",
| "f13",
| "f14",
| "f15",
| "f16",
| "f17",
| "f18",
| "f19",
| "f20",
| "f21",
| "f22",
| "f23"
| ]
| }
| }
| }
|}
|""".stripMargin
assertTrue(
json == toJsonAst(expectedJson),
)
},
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package zio.http.endpoint.openapi

import java.util.UUID

import scala.annotation.tailrec
import scala.collection.immutable.ListMap
import scala.collection.{immutable, mutable}

import zio._
import zio.json.EncoderOps
import zio.json.ast.Json

import zio.schema.Schema.Record
import zio.schema.Schema.{Record, Transform}
import zio.schema.codec.JsonCodec
import zio.schema.{Schema, TypeId}

Expand Down Expand Up @@ -935,6 +936,7 @@ object OpenAPIGen {
}
}

@tailrec
def nominal(schema: Schema[_], referenceType: SchemaStyle): Option[String] =
schema match {
case enumSchema: Schema.Enum[_] =>
Expand All @@ -955,6 +957,8 @@ object OpenAPIGen {
case nominal: TypeId.Nominal =>
Some(nominal.fullyQualified.replace(".", "_"))
}
case t: Transform[_, _, _] =>
nominal(t.schema, referenceType)
case _ => None
}

Expand Down

0 comments on commit 8074422

Please sign in to comment.