Uploaded image for project: 'Scala'
  1. Scala
  2. SCALA-299

Case Class Support: Map not existing field to None



    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 2.0.0
    • Fix Version/s: 2.1.0
    • Component/s: bson
    • Labels:
    • Sprint:
      JVM Sprint 39


      We have used Mongo Scala Driver 1.2 in the past and wrote our own converters from Document to DomainClass.
      For optional values we just didn't include the field in the Document, e.g.:

      case class Person(name: String, nickname: Option[String])

      Person("someName", Some("someNickname")) converts to Document("name" -> "someName", "nickname" -> "someNickname")

      Person("someName", None) converts to Document("name" -> "someName")

      and vise versa:

      Document("name" -> "someName") converts to Person("someName", None)
      Document("name" -> "someName", "someNickname" -> null) also converts to Person("someName", None)

      Thus our Documents in the Database were smaller and we were able to be backward compatible when adding a new optional field to our case classes.

      Now with the case class support we are somehow forced to migrate all our written data, because the codec just throws an exception "key not found" when fields are not present in the database. And we can't simply add a new optional field to our case classes.

      With this little fix in CaseClassCodec none existing optional fields would default to None when reading (line 236):
      fieldData.get($key).filter(_ != null) instead of fieldData($key)

      Is there a good reason that None defaults to BsonNull? Or could it also default to "not there"?

      Thanks for your help




            • Votes:
              0 Vote for this issue
              2 Start watching this issue


              • Created: