Skip to content

Commit 80627f4

Browse files
committed
Fix RUBY-2572 symbolize_keys! when called on a BSON::Document produces a broken object
1 parent 13c42e1 commit 80627f4

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-1
lines changed

lib/bson/document.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ def except(*keys)
315315
copy
316316
end
317317

318+
def symbolize_keys!
319+
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'
320+
end
321+
318322
private
319323

320324
def convert_key(key)

spec/bson/document_as_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (C) 2021 MongoDB Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require "spec_helper"
16+
17+
# BSON::Document ActiveSupport extensions
18+
describe BSON::Document do
19+
require_active_support
20+
21+
describe '#symbolize_keys' do
22+
context 'string keys' do
23+
let(:doc) do
24+
described_class.new('foo' => 'bar')
25+
end
26+
27+
it 'works correctly' do
28+
doc.symbolize_keys.should == {foo: 'bar'}
29+
end
30+
end
31+
end
32+
33+
describe '#symbolize_keys!' do
34+
context 'string keys' do
35+
let(:doc) do
36+
described_class.new('foo' => 'bar')
37+
end
38+
39+
it 'raises ArgumentError' do
40+
lambda do
41+
doc.symbolize_keys!
42+
end.should raise_error(ArgumentError, /symbolize_keys! is not supported on BSON::Document instances/)
43+
end
44+
end
45+
end
46+
end

spec/bson/hash_as_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (C) 2021 MongoDB Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require "spec_helper"
16+
17+
describe 'Hash ActiveSupport extensions' do
18+
require_active_support
19+
20+
describe '#symbolize_keys' do
21+
let(:symbolized) { hash.symbolize_keys }
22+
23+
shared_examples 'works correctly' do
24+
it 'returns a hash' do
25+
symbolized.class.should be Hash
26+
end
27+
28+
it 'works correctly' do
29+
hash.symbolize_keys.should == {foo: 'bar'}
30+
end
31+
end
32+
33+
context 'string keys' do
34+
let(:hash) do
35+
{'foo' => 'bar'}
36+
end
37+
38+
include_examples 'works correctly'
39+
end
40+
41+
context 'symbol keys' do
42+
let(:hash) do
43+
{foo: 'bar'}
44+
end
45+
46+
include_examples 'works correctly'
47+
end
48+
49+
context 'both string and symbol keys' do
50+
let(:hash) do
51+
{'foo' => 42, foo: 'bar'}
52+
end
53+
54+
include_examples 'works correctly'
55+
end
56+
end
57+
end

spec/spec_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
BSON_CORPUS_TESTS = Dir.glob("#{CURRENT_PATH}/spec_tests/data/corpus/*.json").sort
1818
BSON_CORPUS_LEGACY_TESTS = Dir.glob("#{CURRENT_PATH}/spec_tests/data/corpus_legacy/*.json").sort
1919

20+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "shared", "lib"))
2021
$LOAD_PATH.unshift(File.dirname(__FILE__))
2122
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2223

@@ -40,6 +41,8 @@
4041
end
4142
end
4243

44+
require 'mrss/lite_constraints'
45+
4346
Dir["./spec/support/**/*.rb"].each { |file| require file }
4447

4548
# Alternate IO class that returns a String from #readbyte.
@@ -71,4 +74,6 @@ def readbyte
7174
end
7275
end
7376
end
77+
78+
config.extend Mrss::LiteConstraints
7479
end

0 commit comments

Comments
 (0)