-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 2.14.0
-
Component/s: BSON
-
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)