Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-1032

BSON::Document doesn't assign or merge! quite like a Hash

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 12_01_17
    • Affects Version/s: bson-3.2.4
    • Component/s: BSON
    • None
    • Environment:
      Ruby 1.9.3

      I've found this bug through an issue I'm having with Mongoid, so I'm not 100% sure if this is an issue with BSON, or that Mongoid is using it wrong.

      Consider the following code:

      Unable to find source-code formatter for language: example.rb. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      attributes = BSON::Document.new
      attributes["test"] = nil
      (attributes["test"] ||= {}).merge!({"test" => "test})
      # You'd expect attributes["test"] to be {"test" => "test"} 
      # here, but it's actually {}
      

      The last line is found in Mongoid here: https://github.com/mongodb/mongoid/blob/master/lib/mongoid/attributes.rb#L179 and (I think) is the result is causing issues like this: http://stackoverflow.com/questions/29899935/mongoid-localized-field-not-being-set-in-first-form-submission

      It works fine if you make attributes a Hash instead, so I presume it's something the way ||= or merge! are implemented in BSON::Document?

      Writing my example differently, e.g.

      Unable to find source-code formatter for language: example.rb. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      attributes = BSON::Document.new
      attributes["test"] = nil
      attributes["test"] ||= {}
      attributes["test"].merge!({"test" => "test})
      

      also works as expected.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            stevenday Steven Day
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: