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

Custom serialization of fields is broken in v3.0.1

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.2
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Executive summary: custom serialization does not convert objects to the storage type automatically and requires the object to be typecast manually instead

      I used to have a field declared as "CompressedObject". With mongoid v2, this field defined two methods - serialize, which would gzip the content, and deserialize, which would unzip the content. When I switched to the new interface, I declared the new methods on the class and ran into a peculiar problem:

      when I do model_instance.myfield = {:a => 1} and then model_instance.save, CompressedObject::mongoize is not called like before. Instead, mongoid looks for Hash::mongoize. Since the latter is missing, no compression gets done and the value is stored as is (at least according to the Moped logs showing the UPDATE/$set cmd going through). Next, when I try to recover the value by doing model_instance.myfield, this time around it is interpreted as a CompressedObject and the demongoize method is called. Of course, since my value is a plain hash, demongoize blows up.

      By the way, when I try to assign and save {:b=>2}, this is what I get in the log (my field is called 'result'):

      update={"$set"=>{"result"=>{:b=>2}
      

      I am not yet sure if I can solve it on my end. Current behavior means that all my incoming hashes will be interpreted as Hash and unless I override serialization in the Hash class itself, they won't get compressed. Conversely, if I do provide mongoize/demongoize for Hash, then all Hash values will get compressed, even when I don't want that (e.g. when I declare a field as "Hash", rather than "CompressedObject").

            Assignee:
            Unassigned Unassigned
            Reporter:
            emirkin Eugene Mirkin
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: