From e24f38aa83775fc0a3e2a6a8eabe947ba99f6297 Mon Sep 17 00:00:00 2001 From: Paul Daniels Date: Sat, 18 May 2024 01:53:36 +0800 Subject: [PATCH] Fix the json decoder for case classes that only have `None` fields. (#677) Co-authored-by: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> Co-authored-by: Milad Khajavi --- .../src/main/scala/zio/schema/codec/JsonCodec.scala | 2 +- .../scala-2/zio/schema/codec/JsonCodecSpec.scala | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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 f56b99605..73cdac35b 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 @@ -1344,7 +1344,7 @@ object JsonCodec { if (discriminator == -1) { Lexer.char(trace, in, '{') - loop(0, in) + if (Lexer.firstField(trace, in)) loop(0, in) } else if (discriminator == -2) { if (Lexer.nextField(trace, in)) loop(0, in) } else { 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 a52f0bacc..0393c8a7a 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 @@ -1121,6 +1121,9 @@ object JsonCodecSpec extends ZIOSpecDefault { }, test("object") { assertEncodesThenDecodes(schemaObject, Singleton) + }, + test("all optional fields empty") { + assertEncodesThenDecodes(AllOptionalFields.schema, AllOptionalFields(None, None, None)) } ), suite("record")( @@ -1854,4 +1857,14 @@ object JsonCodecSpec extends ZIOSpecDefault { implicit lazy val mapSchema: Schema[Map[KeyWrapper, ValueWrapper]] = Schema.map[KeyWrapper, ValueWrapper] implicit lazy val schema: Schema[MapOfComplexKeysAndValues] = DeriveSchema.gen[MapOfComplexKeysAndValues] } + + final case class AllOptionalFields( + name: Option[String], + mode: Option[Int], + active: Option[Boolean] + ) + + object AllOptionalFields { + implicit lazy val schema: Schema[AllOptionalFields] = DeriveSchema.gen[AllOptionalFields] + } }