Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5609

Better handling of ambiguous type casting

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      This ticket is intended to replace MONGOID-5382.

      Background

      • "mongoizing" means type casting from Ruby to the MongoDB database, e.g. when persisting data
      • "demongoizing" means type casting from the MongoDB database to Ruby, e.g. when querying data
      • MONGOID-5221 / MONGOID-5222 cleaned up various cases of mongoizing/demongoizing for ambigous types. However, a brute force approach was taken where uncastable values (e.g. converting String "Foobar" to Integer) would simply convert as nil. This can lead to inadvertent data-loss.
      • MONGOID-5408 implemented a "Mongoid::RawValue" wrapper class intended to allow querying for raw values in the database.

      Proposal

       
      Instead of mongoizing/demongoizing ambiguous types to nil, we should:

      1. When mongoizing (persisting), raise an InvalidType error when attempting to assign an completely uncastable type, e.g. String "Foobar" to Integer. Note that String "123.456", etc. is still valid, as well as String "" which casts to nil.
      2. When demongoizing (querying), return uncastable values wrapped in Mongoid::RawValue. For example, if the DB contains "Foobar" on an integer "my_number" field, the model will get "my_number" returned as Mongoid::RawValue("Foobar").

       
      Both new behaviors should be wrapped in a respective feature flags.

            Assignee:
            Unassigned Unassigned
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: