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

Add workaround for invalid UndefinedType serialization #704

Merged
merged 4 commits into from
Jul 31, 2024
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
12 changes: 11 additions & 1 deletion lib/src/test/resources/example-union-types-ning-client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,17 @@ package io.apibuilder.example.union.types.v0.models {
obj match {
case x: io.apibuilder.example.union.types.v0.models.Foo => play.api.libs.json.Json.obj("foo" -> play.api.libs.json.JsString(x.toString))
case x: io.apibuilder.example.union.types.v0.models.Bar => play.api.libs.json.Json.obj("bar" -> play.api.libs.json.JsString(x.toString))
case x: io.apibuilder.example.union.types.v0.models.FoobarUndefinedType => sys.error(s"The type[io.apibuilder.example.union.types.v0.models.FoobarUndefinedType] should never be serialized")
case x: io.apibuilder.example.union.types.v0.models.FoobarUndefinedType => {
scala.util.Try {
// If we received a JSON object - echo it back. This is a workaround for a bug in
// serialization for unions w/out discriminators where they sometimes have the
// type wrapper and sometimes do not
play.api.libs.json.Json.parse(x.description).asInstanceOf[play.api.libs.json.JsObject]
} match {
case scala.util.Success(o) => o
case scala.util.Failure(_) => sys.error("The type[io.apibuilder.example.union.types.v0.models.FoobarUndefinedType] should never be serialized")
}
}
}
}
implicit def jsonWritesApidocExampleUnionTypesFoobar: play.api.libs.json.Writes[Foobar] = {
Expand Down
12 changes: 11 additions & 1 deletion lib/src/test/resources/example-union-types-play-23.txt
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,17 @@ package io.apibuilder.example.union.types.v0.models {
obj match {
case x: io.apibuilder.example.union.types.v0.models.Foo => play.api.libs.json.Json.obj("foo" -> play.api.libs.json.JsString(x.toString))
case x: io.apibuilder.example.union.types.v0.models.Bar => play.api.libs.json.Json.obj("bar" -> play.api.libs.json.JsString(x.toString))
case x: io.apibuilder.example.union.types.v0.models.FoobarUndefinedType => sys.error(s"The type[io.apibuilder.example.union.types.v0.models.FoobarUndefinedType] should never be serialized")
case x: io.apibuilder.example.union.types.v0.models.FoobarUndefinedType => {
scala.util.Try {
// If we received a JSON object - echo it back. This is a workaround for a bug in
// serialization for unions w/out discriminators where they sometimes have the
// type wrapper and sometimes do not
play.api.libs.json.Json.parse(x.description).asInstanceOf[play.api.libs.json.JsObject]
} match {
case scala.util.Success(o) => o
case scala.util.Failure(_) => sys.error("The type[io.apibuilder.example.union.types.v0.models.FoobarUndefinedType] should never be serialized")
}
}
}
}
implicit def jsonWritesApidocExampleUnionTypesFoobar: play.api.libs.json.Writes[Foobar] = {
Expand Down
12 changes: 11 additions & 1 deletion lib/src/test/resources/scala-union-enums-json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ def jsObjectUserType(obj: test.apidoc.apidoctest.v0.models.UserType): play.api.l
obj match {
case x: test.apidoc.apidoctest.v0.models.MemberType => play.api.libs.json.Json.obj("member_type" -> play.api.libs.json.JsString(x.toString))
case x: test.apidoc.apidoctest.v0.models.RoleType => play.api.libs.json.Json.obj("role_type" -> play.api.libs.json.JsString(x.toString))
case x: test.apidoc.apidoctest.v0.models.UserTypeUndefinedType => sys.error(s"The type[test.apidoc.apidoctest.v0.models.UserTypeUndefinedType] should never be serialized")
case x: test.apidoc.apidoctest.v0.models.UserTypeUndefinedType => {
scala.util.Try {
// If we received a JSON object - echo it back. This is a workaround for a bug in
// serialization for unions w/out discriminators where they sometimes have the
// type wrapper and sometimes do not
play.api.libs.json.Json.parse(x.description).asInstanceOf[play.api.libs.json.JsObject]
} match {
case scala.util.Success(o) => o
case scala.util.Failure(_) => sys.error("The type[test.apidoc.apidoctest.v0.models.UserTypeUndefinedType] should never be serialized")
}
}
}
}
implicit def jsonWritesAPIBuilderTestUserType: play.api.libs.json.Writes[UserType] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ def jsObjectUser(obj: test.apidoc.apidoctest.v0.models.User): play.api.libs.json
obj match {
case x: test.apidoc.apidoctest.v0.models.RegisteredUser => play.api.libs.json.Json.obj("registered_user" -> test.apidoc.apidoctest.v0.models.json.jsObjectRegisteredUser(x))
case x: test.apidoc.apidoctest.v0.models.GuestUser => play.api.libs.json.Json.obj("guest_user" -> test.apidoc.apidoctest.v0.models.json.jsObjectGuestUser(x))
case x: test.apidoc.apidoctest.v0.models.UserUndefinedType => sys.error(s"The type[test.apidoc.apidoctest.v0.models.UserUndefinedType] should never be serialized")
case x: test.apidoc.apidoctest.v0.models.UserUndefinedType => {
scala.util.Try {
// If we received a JSON object - echo it back. This is a workaround for a bug in
// serialization for unions w/out discriminators where they sometimes have the
// type wrapper and sometimes do not
play.api.libs.json.Json.parse(x.description).asInstanceOf[play.api.libs.json.JsObject]
} match {
case scala.util.Success(o) => o
case scala.util.Failure(_) => sys.error("The type[test.apidoc.apidoctest.v0.models.UserUndefinedType] should never be serialized")
}
}
}
}
implicit def jsonWritesAPIBuilderTestUser: play.api.libs.json.Writes[User] = {
Expand Down
12 changes: 11 additions & 1 deletion lib/src/test/resources/scala-union-models-json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@ def jsObjectUser(obj: test.apidoc.apidoctest.v0.models.User): play.api.libs.json
obj match {
case x: test.apidoc.apidoctest.v0.models.RegisteredUser => play.api.libs.json.Json.obj("registered_user" -> test.apidoc.apidoctest.v0.models.json.jsObjectRegisteredUser(x))
case x: test.apidoc.apidoctest.v0.models.GuestUser => play.api.libs.json.Json.obj("guest_user" -> test.apidoc.apidoctest.v0.models.json.jsObjectGuestUser(x))
case x: test.apidoc.apidoctest.v0.models.UserUndefinedType => sys.error(s"The type[test.apidoc.apidoctest.v0.models.UserUndefinedType] should never be serialized")
case x: test.apidoc.apidoctest.v0.models.UserUndefinedType => {
scala.util.Try {
// If we received a JSON object - echo it back. This is a workaround for a bug in
// serialization for unions w/out discriminators where they sometimes have the
// type wrapper and sometimes do not
play.api.libs.json.Json.parse(x.description).asInstanceOf[play.api.libs.json.JsObject]
} match {
case scala.util.Success(o) => o
case scala.util.Failure(_) => sys.error("The type[test.apidoc.apidoctest.v0.models.UserUndefinedType] should never be serialized")
}
}
}
}
implicit def jsonWritesAPIBuilderTestUser: play.api.libs.json.Writes[User] = {
Expand Down
13 changes: 12 additions & 1 deletion scala-generator/src/main/scala/models/Play2Json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,18 @@ case class Play2Json(
val json = getJsonValueForUnion(t.unionType.datatype, "x")
s"""case x: ${t.typeName} => play.api.libs.json.Json.obj("${t.unionType.discriminatorName}" -> $json)"""
}.mkString("\n").indentString(4),
s""" case x: ${union.undefinedType.datatype.fullName} => sys.error(s"The type[${union.undefinedType.datatype.fullName}] should never be serialized")""",
s"""case x: ${union.undefinedType.datatype.fullName} => {
| scala.util.Try {
| // If we received a JSON object - echo it back. This is a workaround for a bug in
| // serialization for unions w/out discriminators where they sometimes have the
| // type wrapper and sometimes do not
| play.api.libs.json.Json.parse(x.description).asInstanceOf[play.api.libs.json.JsObject]
| } match {
| case scala.util.Success(o) => o
| case scala.util.Failure(_) => sys.error("The type[${union.undefinedType.datatype.fullName}] should never be serialized")
| }
|}
|""".stripMargin.indent(4).stripTrailing(),
" }",
"}"
).mkString("\n")
Expand Down
Loading