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] + } }