From 64050bd23641909a988ae09a48fda690ad40056f Mon Sep 17 00:00:00 2001 From: Juliano Alves Date: Tue, 17 Sep 2024 11:14:32 +0100 Subject: [PATCH] correct implementation of empty for Sequence --- .../zio/schema/codec/JsonCodecSpec.scala | 34 ++++++++++++++++--- .../src/main/scala/zio/schema/Schema.scala | 2 +- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/zio-schema-json/shared/src/test/scala/zio/schema/codec/JsonCodecSpec.scala b/zio-schema-json/shared/src/test/scala/zio/schema/codec/JsonCodecSpec.scala index 65938a6ab..b6dd57558 100644 --- a/zio-schema-json/shared/src/test/scala/zio/schema/codec/JsonCodecSpec.scala +++ b/zio-schema-json/shared/src/test/scala/zio/schema/codec/JsonCodecSpec.scala @@ -1054,15 +1054,15 @@ object JsonCodecSpec extends ZIOSpecDefault { suite("Missing collection fields")( test("map") { assertDecodes( - Schema[ListAndMap], - ListAndMap(Nil, Map.empty), + Schema[ListAndMapAndOption], + ListAndMapAndOption(Nil, Map.empty, None), charSequenceToByteChunk("""{"list":[]}""") ) }, test("list") { assertDecodes( - Schema[ListAndMap], - ListAndMap(Nil, Map.empty), + Schema[ListAndMapAndOption], + ListAndMapAndOption(Nil, Map.empty, None), charSequenceToByteChunk("""{"map":{}}""") ) }, @@ -1072,6 +1072,20 @@ object JsonCodecSpec extends ZIOSpecDefault { SetWrapper(Set.empty), charSequenceToByteChunk("""{}""") ) + }, + test("vector") { + assertDecodes( + Schema[VectorWrapper], + VectorWrapper(Vector.empty), + charSequenceToByteChunk("""{}""") + ) + }, + test("chunck") { + assertDecodes( + Schema[ChunckWrapper], + ChunckWrapper(Chunk.empty), + charSequenceToByteChunk("""{}""") + ) } ), suite("zio.json.ast.Json decoding")( @@ -2201,6 +2215,18 @@ object JsonCodecSpec extends ZIOSpecDefault { implicit lazy val schema: Schema[SetWrapper] = DeriveSchema.gen[SetWrapper] } + final case class VectorWrapper(sequence: Vector[String]) + + object VectorWrapper { + implicit lazy val schema: Schema[VectorWrapper] = DeriveSchema.gen[VectorWrapper] + } + + final case class ChunckWrapper(chunk: Chunk[String]) + + object ChunckWrapper { + implicit lazy val schema: Schema[ChunckWrapper] = DeriveSchema.gen[ChunckWrapper] + } + final case class KeyWrapper(key: String) object KeyWrapper { diff --git a/zio-schema/shared/src/main/scala/zio/schema/Schema.scala b/zio-schema/shared/src/main/scala/zio/schema/Schema.scala index 57ec401aa..56763d16e 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/Schema.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/Schema.scala @@ -535,7 +535,7 @@ object Schema extends SchemaPlatformSpecific with SchemaEquality { override def toString: String = s"Sequence($elementSchema, $identity)" - override def empty: Col = Seq.empty[Elem].asInstanceOf[Col] + override def empty: Col = fromChunk(Chunk.empty[Elem]) } final case class Transform[A, B, I](