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 34e36216f..d746203a2 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 @@ -940,7 +940,7 @@ object JsonCodec { val fieldName = span.field val prev = map.put(fieldName, dec.unsafeDecode(trace_, in)) if (prev != null) { - throw UnsafeJson(JsonError.Message("duplicate") :: trace) + throw UnsafeJson(JsonError.Message("duplicate") :: trace_) } } else if (rejectAdditionalFields) { throw UnsafeJson(JsonError.Message(s"unexpected field: $fieldNameOrAlias") :: trace) @@ -1499,8 +1499,9 @@ object JsonCodec { val idx = Lexer.field(trace, reader, stringMatrix) if (pos == discriminator) Lexer.skipValue(trace, reader) else if (idx >= 0) { - if (buffer(idx) != null) error(trace, "duplicate") - else buffer(idx) = fieldDecoders(idx).unsafeDecode(spans(idx) :: trace, reader) + val trace_ = spans(idx) :: trace + if (buffer(idx) != null) error(trace_, "duplicate") + else buffer(idx) = fieldDecoders(idx).unsafeDecode(trace_, reader) } else if (!rejectExtraFields) Lexer.skipValue(trace, reader) else error(trace, "extra field") continue = Lexer.nextField(trace, reader) 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 c39a20796..3945baccb 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 @@ -721,12 +721,12 @@ object JsonCodecSpec extends ZIOSpecDefault { assertDecodesToError( recordSchema, """{"foo":"test","bar":10,"foo":10}""", - JsonError.Message("duplicate") :: Nil + JsonError.Message("duplicate") :: JsonError.ObjectAccess("foo") :: Nil ) &> assertDecodesToError( recordSchema, """{"bar":10,"foo":"test","bar":"100"}""", - JsonError.Message("duplicate") :: Nil + JsonError.Message("duplicate") :: JsonError.ObjectAccess("bar") :: Nil ) }, test("optional field with schema or annotated default value") { @@ -837,12 +837,12 @@ object JsonCodecSpec extends ZIOSpecDefault { assertDecodesToError( personSchema, """{"name":"test","age":10,"name":10}""", - JsonError.Message("duplicate") :: Nil + JsonError.Message("duplicate") :: JsonError.ObjectAccess("name") :: Nil ) &> assertDecodesToError( personSchema, """{"age":10,"name":"test","age":"100"}""", - JsonError.Message("duplicate") :: Nil + JsonError.Message("duplicate") :: JsonError.ObjectAccess("age") :: Nil ) }, test("transient field annotation with default value in class definition") {