Skip to content

Commit

Permalink
Bugfix: Read fields of non-primitive options correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Feb 27, 2024
1 parent 276b2e6 commit 9bce206
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,6 @@ object JsonCodec {
case 'n' =>
Lexer.readChars(trace, in, ull, "null")
None
case '{' =>
Lexer.readChars(trace, in, "}".toCharArray, "{}")
None
case _ =>
in.retract()
Some(A.unsafeDecode(trace, in))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,20 @@ object JsonCodecSpec extends ZIOSpecDefault {
WithOptionFields(Some("s"), None),
charSequenceToByteChunk("""{"a":"s", "b":{}}""")
)
},
test("case class with complex option field accept empty json object as value") {
assertDecodes(
WithComplexOptionField.schema,
WithComplexOptionField(None),
charSequenceToByteChunk("""{"order":{}}""")
)
},
test("case class with complex option field correctly decodes") {
assertDecodes(
WithComplexOptionField.schema,
WithComplexOptionField(Some(Order(1, BigDecimal.valueOf(10), "test"))),
charSequenceToByteChunk("""{"order":{"id":1,"value":10,"description":"test"}}""")
)
}
),
suite("enums")(
Expand Down Expand Up @@ -1753,6 +1767,12 @@ object JsonCodecSpec extends ZIOSpecDefault {
implicit lazy val schema: Schema[WithOptionFields] = DeriveSchema.gen[WithOptionFields]
}

final case class WithComplexOptionField(order: Option[Order])

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

final case class WithOptField(@optionalField list: List[String], @optionalField map: Map[String, Int])

object WithOptField {
Expand Down

0 comments on commit 9bce206

Please sign in to comment.