From a1d38fc2c4e36edfc7d05361275da615fc580f7f Mon Sep 17 00:00:00 2001 From: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:54:55 +0200 Subject: [PATCH] Annotations on Transform have effects on inner schema for Json Codec (#604) Co-authored-by: Daniel Vigovszky --- .../shared/src/main/scala/zio/schema/codec/JsonCodec.scala | 4 ++-- .../shared/src/main/scala/zio/schema/codec/package.scala | 2 +- 2 files changed, 3 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 1909e78a3..6ee664ad9 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 @@ -171,7 +171,7 @@ object JsonCodec { case Schema.Map(ks, vs, _) => mapEncoder(ks, vs, discriminatorTuple) case Schema.Set(s, _) => ZJsonEncoder.chunk(schemaEncoder(s, discriminatorTuple)).contramap(m => Chunk.fromIterable(m)) - case Schema.Transform(c, _, g, _, _) => transformEncoder(c, g) + case Schema.Transform(c, _, g, a, _) => transformEncoder(a.foldLeft(c)((s, a) => s.annotate(a)), g) case Schema.Tuple2(l, r, _) => ZJsonEncoder.tuple2(schemaEncoder(l, discriminatorTuple), schemaEncoder(r, discriminatorTuple)) case Schema.Optional(schema, _) => ZJsonEncoder.option(schemaEncoder(schema, discriminatorTuple)) case Schema.Fail(_, _) => unitEncoder.contramap(_ => ()) @@ -490,7 +490,7 @@ object JsonCodec { case Schema.Primitive(standardType, _) => primitiveCodec(standardType).decoder case Schema.Optional(codec, _) => ZJsonDecoder.option(schemaDecoder(codec, hasDiscriminator)) case Schema.Tuple2(left, right, _) => ZJsonDecoder.tuple2(schemaDecoder(left, hasDiscriminator), schemaDecoder(right, hasDiscriminator)) - case Schema.Transform(codec, f, _, _, _) => schemaDecoder(codec, hasDiscriminator).mapOrFail(f) + case Schema.Transform(c, f, _, a, _) => schemaDecoder(a.foldLeft(c)((s, a) => s.annotate(a)), hasDiscriminator).mapOrFail(f) case Schema.Sequence(codec, f, _, _, _) => ZJsonDecoder.chunk(schemaDecoder(codec, hasDiscriminator)).map(f) case Schema.Map(ks, vs, _) => mapDecoder(ks, vs, hasDiscriminator) case Schema.Set(s, _) => ZJsonDecoder.chunk(schemaDecoder(s, hasDiscriminator)).map(entries => entries.toSet) diff --git a/zio-schema-json/shared/src/main/scala/zio/schema/codec/package.scala b/zio-schema-json/shared/src/main/scala/zio/schema/codec/package.scala index d1eeb7135..e0e3b781d 100644 --- a/zio-schema-json/shared/src/main/scala/zio/schema/codec/package.scala +++ b/zio-schema-json/shared/src/main/scala/zio/schema/codec/package.scala @@ -11,7 +11,7 @@ import zio.schema.{ DynamicValue, Schema, StandardType, TypeId } package object json { implicit val schemaJson: Schema[Json] = - Schema.dynamicValue.annotate(directDynamicMapping()).transform(toJson, fromJson) + Schema.dynamicValue.transform(toJson, fromJson).annotate(directDynamicMapping()) private def toJson(dv: DynamicValue): Json = dv match {