Skip to content

Commit

Permalink
Fix missing field name in an error for duplicated fields (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
plokhotnyuk authored Jan 6, 2025
1 parent 7629e75 commit c0a2ec7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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") {
Expand Down

0 comments on commit c0a2ec7

Please sign in to comment.