Skip to content

Commit

Permalink
fixed decoder for enum with discriminator (#610)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Vigovszky <[email protected]>
  • Loading branch information
pablf and vigoo authored Nov 18, 2023
1 parent 80172cf commit b0b7062
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ object JsonCodec {
case Schema.Either(left, right, _) => ZJsonDecoder.either(schemaDecoder(left, hasDiscriminator), schemaDecoder(right, hasDiscriminator))
case l @ Schema.Lazy(_) => schemaDecoder(l.schema, hasDiscriminator)
//case Schema.Meta(_, _) => astDecoder
case s @ Schema.CaseClass0(_, _, _) => caseClass0Decoder(s)
case s @ Schema.CaseClass0(_, _, _) => caseClass0Decoder(hasDiscriminator, s)
case s @ Schema.CaseClass1(_, _, _, _) => caseClass1Decoder(hasDiscriminator, s)
case s @ Schema.CaseClass2(_, _, _, _, _) => caseClass2Decoder(hasDiscriminator, s)
case s @ Schema.CaseClass3(_, _, _, _, _, _) => caseClass3Decoder(hasDiscriminator, s)
Expand Down Expand Up @@ -833,8 +833,8 @@ object JsonCodec {
private[codec] object ProductDecoder {
import zio.schema.codec.JsonCodec.JsonDecoder.schemaDecoder

private[codec] def caseClass0Decoder[Z](schema: Schema.CaseClass0[Z]): ZJsonDecoder[Z] = { (trace: List[JsonError], in: RetractReader) =>
val _ = Codecs.unitDecoder.unsafeDecode(trace, in)
private[codec] def caseClass0Decoder[Z](hasDiscriminator: Boolean, schema: Schema.CaseClass0[Z]): ZJsonDecoder[Z] = { (trace: List[JsonError], in: RetractReader) =>
if (!hasDiscriminator) Codecs.unitDecoder.unsafeDecode(trace, in)
schema.defaultConstruct()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
charSequenceToByteChunk("""{"oneOf":{"_type":"StringValue2","value":"foo2"}}""")
)
},
test("case class ") {
test("case class") {
assertEncodes(
searchRequestWithTransientFieldSchema,
SearchRequestWithTransientField("foo", 10, 20, "bar"),
Expand Down Expand Up @@ -1027,6 +1027,10 @@ object JsonCodecSpec extends ZIOSpecDefault {
Enumeration2(BooleanValue2(false))
)
},
test("of case classes with discriminator") {
assertEncodesThenDecodes(Schema[Command], Command.Cash) &>
assertEncodesThenDecodes(Schema[Command], Command.Buy(100))
},
suite("of case objects")(
test("without annotation")(
assertEncodesThenDecodes(Schema[Color], Color.Red)
Expand Down Expand Up @@ -1496,6 +1500,16 @@ object JsonCodecSpec extends ZIOSpecDefault {
implicit val schema: Schema[Color] = DeriveSchema.gen[Color]
}

@annotation.discriminatorName("type")
sealed trait Command

object Command {
case class Buy(credits: Int) extends Command
case object Cash extends Command

implicit val schema: Schema[Command] = DeriveSchema.gen[Command]
}

case object Singleton
implicit val schemaObject: Schema[Singleton.type] = DeriveSchema.gen[Singleton.type]

Expand Down

0 comments on commit b0b7062

Please sign in to comment.