Skip to content

Commit

Permalink
Improve expected tagged dictionary error message and make it work f…
Browse files Browse the repository at this point in the history
…or non-empty dicts (#502)

Fixes #500
  • Loading branch information
lihaoyi committed Jul 9, 2023
1 parent c76d7c6 commit 54f32d2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 5 additions & 2 deletions upickle/src/upickle/Api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,14 @@ trait AttributeTagged extends Api with Annotator{
}
}
def visitEnd(index: Int) = {
if (context == null) throw new Abort("expected tagged dictionary")
def missingKeyMsg = s"""Missing key "$tagName" for tagged dictionary"""
if (context == null) throw new Abort(missingKeyMsg)
else if (fastPath) context.visitEnd(index).asInstanceOf[T]
else{
val x = context.visitEnd(index).asInstanceOf[IndexedValue.Obj]
val keyAttr = x.value0.find(_._1.toString == tagName).get._2
val keyAttr = x.value0.find(_._1.toString == tagName)
.getOrElse(throw new Abort(missingKeyMsg))
._2
val key = keyAttr.asInstanceOf[IndexedValue.Str].value0.toString
val delegate = taggedReader.findReader(key)
if (delegate == null){
Expand Down
3 changes: 2 additions & 1 deletion upickle/test/src/upickle/FailureTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ object FailureTests extends TestSuite {
test - assertErrorMsgDefault[Fi.Fo]("""{"$type": "omg"}""", "invalid tag for tagged object: omg at index 1")
test - assertErrorMsgDefault[Fi](""""omg"""", "invalid tag for tagged object: omg at index 0")
test - assertErrorMsgDefault[Fi]("""{"$type": "omg"}""", "invalid tag for tagged object: omg at index 1")
test - assertErrorMsgDefault[Fi]("""{}""", "expected tagged dictionary")
test - assertErrorMsgDefault[Fi]("""{}""", """Missing key "$type" for tagged dictionary at index 1""")
test - assertErrorMsgDefault[Fi]("""{"mispelledTypeTag": "omg"}""", """Missing key "$type" for tagged dictionary at index 26""")
}

test("taggedInvalidBody"){
Expand Down

0 comments on commit 54f32d2

Please sign in to comment.