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

BSON::Document#except does not exclude symbol arguments with ActiveSupport 6

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • bson-4.12.0
    • Affects Version/s: 2.14.0
    • Component/s: BSON
    • Labels:
      None
    • Environment:
      ruby 2.6
    •  BSON::Document#except now works with both string and symbol keys regardless of which ActiveSupport version is loaded (if any). Also, the return value of #except is now always a BSON::Document (providing indifferent access to keys) rather than Hash.
    • Minor Change

      What is the expected behaviour of BSON::Document#except when the arguments are symbols? I'm observing this behaviour

       

      require 'bundler/inline'
      gemfile do
        source 'https://rubygems.org'
        gem 'mongo', '2.14.0'
        gem 'bson', '4.11.1'
        gem 'activesupport', '~> 5.2'
      end
      require 'active_support/core_ext/hash'
      require 'bson'
      doc = BSON::Document.new("key" => "value")
      doc.except(:key) #=> {}
      

       

       

       

      require 'bundler/inline'
      gemfile do
        source 'https://rubygems.org'
        gem 'mongo', '2.14.0'
        gem 'bson', '4.11.1'
        gem 'activesupport', '~> 6.0.3'
      end
      require 'active_support/core_ext/hash'
      require 'bson'
      doc = BSON::Document.new("key" => "value")
      doc.except(:key) #=> {"key"=>"value"}
      

       

      i.e. the behaviour depends on the version of activesupport loaded.

      This looks to be because in 5.2 `except` in active support was implemented by duping the hash and then calling `delete` on all the passed arguments. Since {{BSON::Document }}implements delete to convert keys to strings, except inherits the behavior.

      In rails 6, except is defined as

      def except(*keys)
        slice(*self.keys - keys)
      end
      

       

      And so keys are not converted.

      It would be straightforward to implement except directly on BSON::Document }}(and I'd be happy to do a PR for that){{ }}but this does at least come down in part to what should it do, since it many ways this is a another project's monkey patch to Hash causing issues (however in ruby 3 {{except is part of Hash)

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            fred@dressipi.com Frederick Cheung
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: