Error serializing record with field value of "{}"
Closed this issue · 2 comments
davegurnell commented
There's some seemingly erroneous behaviour trying to serialize JSON objects to BSON when they have field values that are empty objects.
Here's some REPL output to illustrate:
scala> import play.api.libs.json._,
play.modules.reactivemongo.json.BSONFormats._,
reactivemongo.bson._
import play.api.libs.json._
import play.modules.reactivemongo.json.BSONFormats._
import reactivemongo.bson._
scala> Json.fromJson[BSONDocument](Json.obj("a" -> Json.obj()))
res0: play.api.libs.json.JsResult[reactivemongo.bson.BSONDocument] =
JsError(List((,List(ValidationError(List((/$regex,List(ValidationError(string expected,WrappedArray()))), (/$options,List(ValidationError(string expected,WrappedArray())))),WrappedArray())))))
I'd expect this object to be serializable. However, the JsError
above causes collection.save()
to throw an exception as follows:
play.api.UnexpectedException: Unexpected exception[NoSuchElementException: JsError.get]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:148) ~[play_2.10-2.2.1.jar:2.2.1]
// ... I've removed the rest of the stack trace for this wrapper exception ...
Caused by: java.util.NoSuchElementException: JsError.get
at play.api.libs.json.JsError.get(JsResult.scala:11) ~[play-json_2.10-2.2.1.jar:2.2.1]
at play.api.libs.json.JsError.get(JsResult.scala:10) ~[play-json_2.10-2.2.1.jar:2.2.1]
at play.modules.reactivemongo.json.collection.JSONGenericHandlers$StructureBufferWriter$.write(jsoncollection.scala:44) ~[play2-reactivemongo_2.10-0.10.0.jar:0.10.0]
at play.modules.reactivemongo.json.collection.JSONGenericHandlers$StructureBufferWriter$.write(jsoncollection.scala:42) ~[play2-reactivemongo_2.10-0.10.0.jar:0.10.0]
at reactivemongo.api.collections.GenericHandlers$GenericBufferWriter.write(genericcollection.scala:61) ~[reactivemongo_2.10-0.10.0.jar:0.10.0]
at reactivemongo.api.collections.GenericCollection$class.reactivemongo$api$collections$GenericCollection$$writeDoc(genericcollection.scala:97) ~[reactivemongo_2.10-0.10.0.jar:0.10.0]
at reactivemongo.api.collections.GenericCollection$$anonfun$update$1.apply(genericcollection.scala:200) ~[reactivemongo_2.10-0.10.0.jar:0.10.0]
at reactivemongo.api.collections.GenericCollection$$anonfun$update$1.apply(genericcollection.scala:196) ~[reactivemongo_2.10-0.10.0.jar:0.10.0]
at scala.util.Try$.apply(Try.scala:161) ~[scala-library-2.10.3.jar:na]
at reactivemongo.api.collections.GenericCollection$class.watchFailure(genericcollection.scala:99) ~[reactivemongo_2.10-0.10.0.jar:0.10.0]
at play.modules.reactivemongo.json.collection.JSONCollection.watchFailure(jsoncollection.scala:70) ~[play2-reactivemongo_2.10-0.10.0.jar:0.10.0]
at reactivemongo.api.collections.GenericCollection$class.update(genericcollection.scala:196) ~[reactivemongo_2.10-0.10.0.jar:0.10.0]
at play.modules.reactivemongo.json.collection.JSONCollection.update(jsoncollection.scala:70) ~[play2-reactivemongo_2.10-0.10.0.jar:0.10.0]
at play.modules.reactivemongo.json.collection.JSONCollection.save(jsoncollection.scala:96) ~[play2-reactivemongo_2.10-0.10.0.jar:0.10.0]
at play.modules.reactivemongo.json.collection.JSONCollection.save(jsoncollection.scala:107) ~[play2-reactivemongo_2.10-0.10.0.jar:0.10.0]
// ... I've removed the application code from this stack trace ...
sgodbillon commented
Fixed in 2ae6b3d.
I published 0.10.1-SNAPSHOT that contains this patch :)
davegurnell commented
Thank you!