Skip to content

Commit

Permalink
fix: decode ADT with 3 cases with noDiscriminator
Browse files Browse the repository at this point in the history
  • Loading branch information
runtologist committed Apr 22, 2024
1 parent 28930b3 commit 583f8ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ object JsonCodec {

(trace: List[JsonError], in: RetractReader) => {
if (noDiscriminators) {
val rr = RecordingReader(in)
var rr = RecordingReader(in)
val it = cases.iterator
var result: Option[Z] = None

Expand All @@ -723,6 +723,7 @@ object JsonCodec {
} catch {
case _: Exception =>
rr.rewind()
rr = RecordingReader(rr)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,18 @@ object JsonCodecSpec extends ZIOSpecDefault {
Enumeration2(BooleanValue2(false))
)
},
test("ADT with noDiscriminator") {
assertEncodesThenDecodes(
Schema[Enumeration3],
Enumeration3(StringValue3("foo"))
) &> assertEncodesThenDecodes(
Schema[Enumeration3],
Enumeration3(StringValue3Multi("foo", "bar"))
) &> assertEncodesThenDecodes(Schema[Enumeration3], Enumeration3(IntValue3(-1))) &> assertEncodesThenDecodes(
Schema[Enumeration3],
Enumeration3(BooleanValue3(false))
) &> assertEncodesThenDecodes(Schema[Enumeration3], Enumeration3(Nested(StringValue3("foo"))))
},
test("of case classes with discriminator") {
assertEncodesThenDecodes(Schema[Command], Command.Cash) &>
assertEncodesThenDecodes(Schema[Command], Command.Buy(100))
Expand Down Expand Up @@ -1673,6 +1685,20 @@ object JsonCodecSpec extends ZIOSpecDefault {
implicit val schema: Schema[Enumeration2] = DeriveSchema.gen[Enumeration2]
}

@noDiscriminator
sealed trait OneOf3
case class StringValue3(value: String) extends OneOf3
case class IntValue3(value: Int) extends OneOf3
case class BooleanValue3(value: Boolean) extends OneOf3
case class StringValue3Multi(value1: String, value2: String) extends OneOf3
case class Nested(oneOf: OneOf3) extends OneOf3

case class Enumeration3(oneOf: OneOf3)

object Enumeration3 {
implicit val schema: Schema[Enumeration3] = DeriveSchema.gen[Enumeration3]
}

sealed trait Color

object Color {
Expand Down

0 comments on commit 583f8ac

Please sign in to comment.