From d61ef61f367338c0ac06ffd062564d1ca1aeeeca Mon Sep 17 00:00:00 2001 From: Ivan Takarlikov Date: Fri, 18 Apr 2025 14:59:16 -0300 Subject: [PATCH 1/2] Add error raise for deep_symbolize_keys! --- lib/bson/document.rb | 4 ++++ spec/bson/document_as_spec.rb | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/bson/document.rb b/lib/bson/document.rb index f46cab549..0926351aa 100644 --- a/lib/bson/document.rb +++ b/lib/bson/document.rb @@ -321,6 +321,10 @@ def symbolize_keys! raise ArgumentError, 'symbolize_keys! is not supported on BSON::Document instances. Please convert the document to hash first (using #to_h), then call #symbolize_keys! on the Hash instance' end + def deep_symbolize_keys! + raise ArgumentError, 'deep_symbolize_keys! is not supported on BSON::Document instances. Please convert the document to hash first (using #to_h), then call #deep_symbolize_keys! on the Hash instance' + end + # Override the Hash implementation of to_bson_normalized_value. # # BSON::Document is already of the correct type and already provides diff --git a/spec/bson/document_as_spec.rb b/spec/bson/document_as_spec.rb index 984c116c9..4ca1c877b 100644 --- a/spec/bson/document_as_spec.rb +++ b/spec/bson/document_as_spec.rb @@ -44,4 +44,18 @@ end end end + + describe '#deep_symbolize_keys!' do + context 'string keys' do + let(:doc) do + described_class.new('foo' => 'bar') + end + + it 'raises ArgumentError' do + lambda do + doc.deep_symbolize_keys! + end.should raise_error(ArgumentError, /deep_symbolize_keys! is not supported on BSON::Document instances/) + end + end + end end From 9f786e73faede59bcb95466b5ff7d7a85bf2564a Mon Sep 17 00:00:00 2001 From: Ivan Takarlikov Date: Wed, 23 Apr 2025 12:57:39 -0300 Subject: [PATCH 2/2] Warn instead of raise --- lib/bson/document.rb | 6 +++++- spec/bson/document_as_spec.rb | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/bson/document.rb b/lib/bson/document.rb index 0926351aa..7321c5b67 100644 --- a/lib/bson/document.rb +++ b/lib/bson/document.rb @@ -322,7 +322,11 @@ def symbolize_keys! end def deep_symbolize_keys! - raise ArgumentError, 'deep_symbolize_keys! is not supported on BSON::Document instances. Please convert the document to hash first (using #to_h), then call #deep_symbolize_keys! on the Hash instance' + warn <<~WARN + [DEPRECATION] `deep_symbolize_keys!` is not supported on BSON::Document instances. + Please convert the document to a Hash first (using `#to_h`), then call `#deep_symbolize_keys!` on the Hash. + This will raise an error starting with the v6.0.0 release. + WARN end # Override the Hash implementation of to_bson_normalized_value. diff --git a/spec/bson/document_as_spec.rb b/spec/bson/document_as_spec.rb index 4ca1c877b..6ff82ad39 100644 --- a/spec/bson/document_as_spec.rb +++ b/spec/bson/document_as_spec.rb @@ -52,9 +52,9 @@ end it 'raises ArgumentError' do - lambda do + expect do doc.deep_symbolize_keys! - end.should raise_error(ArgumentError, /deep_symbolize_keys! is not supported on BSON::Document instances/) + end.to output(/\[DEPRECATION\] `deep_symbolize_keys!` is not supported on BSON::Document instances./).to_stderr end end end