File tree Expand file tree Collapse file tree 2 files changed +25
-6
lines changed
main/scala/scalapb/json4s
test/scala/scalapb/json4s Expand file tree Collapse file tree 2 files changed +25
-6
lines changed Original file line number Diff line number Diff line change @@ -651,7 +651,7 @@ class Parser private (config: Parser.ParserConfig) {
651
651
652
652
PMessage (valueMapBuilder.result())
653
653
case _ =>
654
- throw new JsonFormatException (s " Expected an object, found ${value}" )
654
+ throw new JsonFormatException (s " Expected an object for ${cmp.scalaDescriptor.fullName} , found ${value}" )
655
655
}
656
656
}
657
657
}
@@ -723,11 +723,19 @@ class Parser private (config: Parser.ParserConfig) {
723
723
res.fold[PValue ](PEmpty )(PEnum .apply)
724
724
}
725
725
case ScalaType .Message (md) =>
726
- fromJsonToPMessage(
727
- containerCompanion.messageCompanionForFieldNumber(fd.number),
728
- value,
729
- false
730
- )
726
+ try {
727
+ fromJsonToPMessage(
728
+ containerCompanion.messageCompanionForFieldNumber(fd.number),
729
+ value,
730
+ false
731
+ )
732
+ } catch {
733
+ case ex : JsonFormatException =>
734
+ throw new JsonFormatException (
735
+ s " Failed parsing field ${fd.name}: ${ex.getMessage}" ,
736
+ ex
737
+ )
738
+ }
731
739
case st =>
732
740
JsonFormat .parsePrimitive(
733
741
fd.protoType,
Original file line number Diff line number Diff line change @@ -456,6 +456,17 @@ class JsonFormatSpec
456
456
)
457
457
}
458
458
459
+ " TestProto" should " fail to parse a field of type Message when it's not a json object" in {
460
+ val jsonFormatException = intercept[JsonFormatException ] {
461
+ new Parser ().fromJsonString[MyTest ](
462
+ """ {"optMessage": 39}"""
463
+ )
464
+ }
465
+ jsonFormatException.getMessage must be (
466
+ " Failed parsing field opt_message: Expected an object for jsontest.MyTest, found JInt(39)"
467
+ )
468
+ }
469
+
459
470
" TestProto" should " parse original field names" in {
460
471
new Parser ().fromJsonString[MyTest ](""" {"opt_enum":1}""" ) must be(
461
472
MyTest (optEnum = Some (MyEnum .V1 ))
You can’t perform that action at this time.
0 commit comments