Skip to content

Commit

Permalink
add caseclass0 check
Browse files Browse the repository at this point in the history
  • Loading branch information
pablf committed Jun 30, 2024
1 parent 91899ff commit fdc60cc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,9 @@ object JsonCodec {

private def enumEncoder[Z](schema: Schema.Enum[Z], cfg: Config, cases: Schema.Case[Z, _]*): ZJsonEncoder[Z] =
// if all cases are CaseClass0, encode as a String
if (schema.annotations.exists(_.isInstanceOf[simpleEnum])) {
if (schema.annotations.exists(_.isInstanceOf[simpleEnum]) && cases.forall(
_.schema.isInstanceOf[Schema.CaseClass0[_]]
)) {
val caseMap: Map[Z, String] =
schema.nonTransientCases
.map(
Expand Down Expand Up @@ -699,7 +701,9 @@ object JsonCodec {
}.toMap

// if all cases are CaseClass0, decode as String
if (cases.forall(_.schema.isInstanceOf[Schema.CaseClass0[_]])) {
if (parentSchema.annotations.exists(_.isInstanceOf[simpleEnum]) && cases.forall(
_.schema.isInstanceOf[Schema.CaseClass0[_]]
)) {
val caseMap: Map[String, Z] =
cases.map(case_ => case_.id -> case_.schema.asInstanceOf[Schema.CaseClass0[Z]].defaultConstruct()).toMap
ZJsonDecoder.string.mapOrFail(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ object JsonCodecSpec extends ZIOSpecDefault {
charSequenceToByteChunk("""{"oneOf":{"_type":"StringValue2","value":"foo2"}}""")
)
},
test("ADT with intermediate enumeration") {
assertEncodes(
Schema[IntermediateEnum],
IntermediateEnum.FinalElem,
charSequenceToByteChunk(""""FinalElem"""")
)
},
test("case class") {
assertEncodes(
searchRequestWithTransientFieldSchema,
Expand Down Expand Up @@ -1226,6 +1233,12 @@ object JsonCodecSpec extends ZIOSpecDefault {
Enumeration2(BooleanValue2(false))
)
},
test("ADT with intermediate enumeration") {
assertEncodesThenDecodes(
Schema[IntermediateEnum],
IntermediateEnum.FinalElem
)
},
test("ADT with noDiscriminator") {
assertEncodesThenDecodes(
Schema[Enumeration3],
Expand Down Expand Up @@ -1742,6 +1755,14 @@ object JsonCodecSpec extends ZIOSpecDefault {
implicit val schema: Schema[Enumeration3] = DeriveSchema.gen[Enumeration3]
}

sealed trait IntermediateEnum

object IntermediateEnum {
sealed trait IntermediateElem extends IntermediateEnum
case object FinalElem extends IntermediateEnum
implicit val schema: zio.schema.Schema[IntermediateEnum] = zio.schema.DeriveSchema.gen[IntermediateEnum]
}

sealed trait Color

object Color {
Expand Down

0 comments on commit fdc60cc

Please sign in to comment.