diff --git a/README.md b/README.md index 758a41291..5c2ddaf82 100644 --- a/README.md +++ b/README.md @@ -39,17 +39,17 @@ _ZIO Schema_ is used by a growing number of ZIO libraries, including [ZIO Flow]( In order to use this library, we need to add the following lines in our `build.sbt` file: ```scala -libraryDependencies += "dev.zio" %% "zio-schema" % "0.4.16" -libraryDependencies += "dev.zio" %% "zio-schema-avro" % "0.4.16" -libraryDependencies += "dev.zio" %% "zio-schema-bson" % "0.4.16" -libraryDependencies += "dev.zio" %% "zio-schema-json" % "0.4.16" -libraryDependencies += "dev.zio" %% "zio-schema-msg-pack" % "0.4.16" -libraryDependencies += "dev.zio" %% "zio-schema-protobuf" % "0.4.16" -libraryDependencies += "dev.zio" %% "zio-schema-thrift" % "0.4.16" -libraryDependencies += "dev.zio" %% "zio-schema-zio-test" % "0.4.16" +libraryDependencies += "dev.zio" %% "zio-schema" % "0.4.17" +libraryDependencies += "dev.zio" %% "zio-schema-avro" % "0.4.17" +libraryDependencies += "dev.zio" %% "zio-schema-bson" % "0.4.17" +libraryDependencies += "dev.zio" %% "zio-schema-json" % "0.4.17" +libraryDependencies += "dev.zio" %% "zio-schema-msg-pack" % "0.4.17" +libraryDependencies += "dev.zio" %% "zio-schema-protobuf" % "0.4.17" +libraryDependencies += "dev.zio" %% "zio-schema-thrift" % "0.4.17" +libraryDependencies += "dev.zio" %% "zio-schema-zio-test" % "0.4.17" // Required for the automatic generic derivation of schemas -libraryDependencies += "dev.zio" %% "zio-schema-derivation" % "0.4.16" +libraryDependencies += "dev.zio" %% "zio-schema-derivation" % "0.4.17" libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value % "provided" ``` diff --git a/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala b/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala index 1f28f6fb9..7f917c6b6 100644 --- a/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala +++ b/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala @@ -274,7 +274,7 @@ object DefaultValueSpec extends ZIOSpecDefault { (s: Status) => s.isInstanceOf[Pending.type] ) ) - assert(schema.defaultValue)(isRight(equalTo(Failed(0, "", None, "")))) + assert(schema.defaultValue)(isRight(equalTo(Failed(0, "", None, "oops")))) } ), suite("EitherSchema")( diff --git a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSpec.scala b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSpec.scala index 0bd2ea19f..80a5b62e2 100644 --- a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSpec.scala +++ b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSpec.scala @@ -170,8 +170,11 @@ import zio.{ Chunk, Scope } .fields(0) .annotations assertTrue( - annotations - .exists(a => a.isInstanceOf[fieldDefaultValue[_]] && a.asInstanceOf[fieldDefaultValue[Int]].value == 42) + annotations.exists { a => + a.isInstanceOf[fieldDefaultValue[_]] && + a.asInstanceOf[fieldDefaultValue[Int]].value == 42 + }, + capturedSchema.schema.defaultValue == Right(RecordWithDefaultValue(42, 52)) ) }, test("use case class default values of generic class") { @@ -180,12 +183,13 @@ import zio.{ Chunk, Scope } .asInstanceOf[Schema.Record[GenericRecordWithDefaultValue[Int]]] .fields(0) .annotations - assertTrue { + assertTrue( annotations.exists { a => a.isInstanceOf[fieldDefaultValue[_]] && a.asInstanceOf[fieldDefaultValue[Option[Int]]].value == None - } - } + }, + capturedSchema.schema.defaultValue == Right(GenericRecordWithDefaultValue[Int](None, 52)) + ) }, test("prefer field annotations over case class default values") { val capturedSchema = Derive.derive[CapturedSchema, RecordWithDefaultValue](schemaCapturer) @@ -195,7 +199,8 @@ import zio.{ Chunk, Scope } .annotations assertTrue( annotations - .exists(a => a.isInstanceOf[fieldDefaultValue[_]] && a.asInstanceOf[fieldDefaultValue[Int]].value == 52) + .exists(a => a.isInstanceOf[fieldDefaultValue[_]] && a.asInstanceOf[fieldDefaultValue[Int]].value == 52), + capturedSchema.schema.defaultValue == Right(RecordWithDefaultValue(42, 52)) ) } ), 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 c4d3166d4..7fe459798 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/Schema.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/Schema.scala @@ -6,6 +6,7 @@ import java.time.temporal.ChronoUnit import scala.annotation.tailrec import scala.collection.immutable.ListMap +import zio.schema.annotation.fieldDefaultValue import zio.schema.internal.SourceLocation import zio.schema.meta._ import zio.schema.validation._ @@ -424,9 +425,12 @@ object Schema extends SchemaEquality { def defaultValue: scala.util.Either[String, R] = Unsafe.unsafe { implicit unsafe => - self.fields - .map(_.schema.defaultValue) - .foldLeft[scala.util.Either[String, Chunk[R]]](Right(Chunk.empty)) { + self.fields.map { field => + field.annotations.collectFirst { case fieldDefaultValue(value) => value } match { + case Some(value) => Right(value) + case None => field.schema.defaultValue + } + }.foldLeft[scala.util.Either[String, Chunk[R]]](Right(Chunk.empty)) { case (e @ Left(_), _) => e case (_, Left(e)) => Left[String, Chunk[R]](e) case (Right(values), Right(value)) => Right[String, Chunk[R]](values :+ value.asInstanceOf[R])