Skip to content

Commit

Permalink
fix: decode ADT with 3 cases with noDiscriminator (#678)
Browse files Browse the repository at this point in the history
* fix: decode ADT with 3 cases with noDiscriminator

* generate readme
  • Loading branch information
runtologist committed Apr 22, 2024
1 parent 28930b3 commit ad0275d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ _ZIO Schema_ is used by a growing number of ZIO libraries, including [ZIO Flow](
In order to use this library, we need to add the following lines in our `build.sbt` file:

```scala
libraryDependencies += "dev.zio" %% "zio-schema" % "1.0.1"
libraryDependencies += "dev.zio" %% "zio-schema-avro" % "1.0.1"
libraryDependencies += "dev.zio" %% "zio-schema-bson" % "1.0.1"
libraryDependencies += "dev.zio" %% "zio-schema-json" % "1.0.1"
libraryDependencies += "dev.zio" %% "zio-schema-msg-pack" % "1.0.1"
libraryDependencies += "dev.zio" %% "zio-schema-protobuf" % "1.0.1"
libraryDependencies += "dev.zio" %% "zio-schema-thrift" % "1.0.1"
libraryDependencies += "dev.zio" %% "zio-schema-zio-test" % "1.0.1"
libraryDependencies += "dev.zio" %% "zio-schema" % "1.1.0"
libraryDependencies += "dev.zio" %% "zio-schema-avro" % "1.1.0"
libraryDependencies += "dev.zio" %% "zio-schema-bson" % "1.1.0"
libraryDependencies += "dev.zio" %% "zio-schema-json" % "1.1.0"
libraryDependencies += "dev.zio" %% "zio-schema-msg-pack" % "1.1.0"
libraryDependencies += "dev.zio" %% "zio-schema-protobuf" % "1.1.0"
libraryDependencies += "dev.zio" %% "zio-schema-thrift" % "1.1.0"
libraryDependencies += "dev.zio" %% "zio-schema-zio-test" % "1.1.0"

// Required for the automatic generic derivation of schemas
libraryDependencies += "dev.zio" %% "zio-schema-derivation" % "1.0.1"
libraryDependencies += "dev.zio" %% "zio-schema-derivation" % "1.1.0"
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value % "provided"
```

Expand Down
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()
if (result.isEmpty && it.hasNext) 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 ad0275d

Please sign in to comment.