Skip to content

Commit 17b63a9

Browse files
Write concern should not have nil or extra values
RUBY-740
1 parent b19033e commit 17b63a9

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

lib/mongo/functional/uri_parser.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ def connection_options
234234
warn "Using wtimeout in a URI is deprecated, please use wtimeoutMS. It will be removed in v2.0."
235235
opts[:wtimeout] = @wtimeout
236236
end
237-
opts[:wtimeout] = @wtimeoutms
237+
opts[:wtimeout] = @wtimeoutms if @wtimeoutms
238238

239239
opts[:w] = 1 if @safe
240240
opts[:w] = @w if @w
241-
opts[:j] = @journal
242-
opts[:fsync] = @fsync
241+
opts[:j] = @journal if @journal
242+
opts[:fsync] = @fsync if @fsync
243243

244244
opts[:connect_timeout] = @connecttimeoutms if @connecttimeoutms
245245
opts[:op_timeout] = @sockettimeoutms if @sockettimeoutms
@@ -260,7 +260,7 @@ def connection_options
260260

261261
opts[:db_name] = @db_name if @db_name
262262
opts[:auths] = @auths if @auths
263-
opts[:ssl] = @ssl
263+
opts[:ssl] = @ssl if @ssl
264264
opts[:connect] = connect?
265265

266266
opts

lib/mongo/functional/write_concern.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
module Mongo
1616
module WriteConcern
17+
VALID_KEYS = [:w, :j, :fsync, :wtimeout]
18+
DEFAULT_WRITE_CONCERN = {:w => 1}
1719

1820
attr_reader :legacy_write_concern
1921

@@ -44,14 +46,9 @@ def write_concern_from_legacy(opts)
4446
# todo: throw exception for conflicting write concern options
4547
def get_write_concern(opts, parent=nil)
4648
write_concern_from_legacy(opts) if opts.key?(:safe) || legacy_write_concern
47-
write_concern = {
48-
:w => 1,
49-
:j => false,
50-
:fsync => false,
51-
:wtimeout => nil
52-
}
49+
write_concern = DEFAULT_WRITE_CONCERN.dup
5350
write_concern.merge!(parent.write_concern) if parent
54-
write_concern.merge!(opts.reject {|k,v| !write_concern.keys.include?(k)})
51+
write_concern.merge!(opts.reject {|k,v| !VALID_KEYS.include?(k)})
5552
write_concern[:w] = write_concern[:w].to_s if write_concern[:w].is_a?(Symbol)
5653
write_concern
5754
end

test/functional/client_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ def test_db_from_uri_from_string_param_no_db_name
144144
assert_equal db.name, MongoClient::DEFAULT_DB_NAME
145145
end
146146

147+
def test_from_uri_write_concern
148+
con = MongoClient.from_uri("mongodb://#{host_port}")
149+
db = con.db
150+
coll = db.collection('from-uri-test')
151+
assert_equal BSON::ObjectId, coll.insert({'a' => 1}).class
152+
[con, db, coll].each do |component|
153+
component.write_concern.each do |key,value|
154+
assert_not_nil(value, "component #{component.class.inspect} should not have write concern #{key.inspect} field with nil value")
155+
end
156+
end
157+
assert_equal({:w => 1}, con.write_concern, "write concern should not have extra pairs that were not specified by the user")
158+
assert_equal({:w => 1}, db.write_concern, "write concern should not have extra pairs that were not specified by the user")
159+
assert_equal({:w => 1}, coll.write_concern, "write concern should not have extra pairs that were not specified by the user")
160+
end
147161

148162
def test_server_version
149163
assert_match(/\d\.\d+(\.\d+)?/, @client.server_version.to_s)

0 commit comments

Comments
 (0)