Skip to content

Commit 01efc42

Browse files
p-mongop
andauthored
Fix RUBY-2273 Regexp::Raw cannot be deserialized from YAML (#201)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 2d2cabf commit 01efc42

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/bson/regexp.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,13 @@ def initialize(pattern, options = '')
165165
#
166166
# @since 3.1.0
167167
def respond_to?(method, include_private = false)
168-
compile.respond_to?(method, include_private) || super
168+
if defined?(@pattern)
169+
compile.respond_to?(method, include_private) || super
170+
else
171+
# YAML calls #respond_to? during deserialization, before the object
172+
# is initialized.
173+
super
174+
end
169175
end
170176

171177
# Encode the Raw Regexp object to BSON.

spec/bson/raw_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,15 @@
580580
end
581581
end
582582
end
583-
end
583+
584+
describe 'yaml loading' do
585+
let(:regexp) { described_class.new('hello.world', 's') }
586+
587+
it 'round-trips' do
588+
actual = YAML.load(regexp.to_yaml)
589+
actual.pattern.should == 'hello.world'
590+
actual.options.should == 's'
591+
actual.compile.should =~ "hello\nworld"
592+
end
593+
end
594+
end

0 commit comments

Comments
 (0)