From 9bce206787ee8c54446c129fd4da4a87ae5bfd0f Mon Sep 17 00:00:00 2001 From: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> Date: Tue, 27 Feb 2024 08:08:54 +0100 Subject: [PATCH] Bugfix: Read fields of non-primitive options correctly --- .../scala/zio/schema/codec/JsonCodec.scala | 3 --- .../zio/schema/codec/JsonCodecSpec.scala | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala b/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala index de1294982..799702b45 100644 --- a/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala +++ b/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala @@ -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)) diff --git a/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala b/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala index 208aa9d02..243ef029e 100644 --- a/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala +++ b/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala @@ -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")( @@ -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 {