File tree Expand file tree Collapse file tree 3 files changed +45
-9
lines changed Expand file tree Collapse file tree 3 files changed +45
-9
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,8 @@ def payload
147147 #
148148 # @since 2.5.0
149149 def serialize ( buffer = BSON ::ByteBuffer . new , max_bson_size = nil )
150+ validate_document_size! ( max_bson_size )
151+
150152 super
151153 add_check_sum ( buffer )
152154 buffer
@@ -275,6 +277,23 @@ def bulk_write?
275277
276278 private
277279
280+ # Validate that the documents in this message are all smaller than the
281+ # maxBsonObjectSize. If not, raise an exception.
282+ def validate_document_size! ( max_bson_size )
283+ max_bson_size ||= Mongo ::Server ::ConnectionBase ::DEFAULT_MAX_BSON_OBJECT_SIZE
284+
285+ contains_too_large_document = @sections . any? do |section |
286+ section [ :type ] == 1 &&
287+ section [ :payload ] [ :sequence ] . any? do |document |
288+ document . to_bson . length > max_bson_size
289+ end
290+ end
291+
292+ if contains_too_large_document
293+ raise Error ::MaxBSONSize . new ( 'The document exceeds maximum allowed BSON object size after serialization' )
294+ end
295+ end
296+
278297 def command
279298 @command ||= if @main_document
280299 @main_document . dup . tap do |cmd |
Original file line number Diff line number Diff line change 113113 it 'raises an exception' do
114114 expect do
115115 bulk_write . execute
116- end . to raise_error ( Mongo ::Error ::MaxBSONSize , /maximum allowed size: 16777216 bytes / )
116+ end . to raise_error ( Mongo ::Error ::MaxBSONSize , /The document exceeds maximum allowed BSON object size after serialization / )
117117 end
118118 end
119119 end
255255 it 'raises an exception' do
256256 expect do
257257 bulk_write . execute
258- end . to raise_error ( Mongo ::Error ::MaxBSONSize , /maximum allowed size: 16777216 bytes / )
258+ end . to raise_error ( Mongo ::Error ::MaxBSONSize , /The document exceeds maximum allowed BSON object size after serialization / )
259259 end
260260 end
261261 end
346346 it 'raises an exception' do
347347 expect do
348348 perform_bulk_write
349- end . to raise_error ( Mongo ::Error ::MaxBSONSize , /maximum allowed size: 16777216 bytes / )
349+ end . to raise_error ( Mongo ::Error ::MaxBSONSize , /The document exceeds maximum allowed BSON object size after serialization / )
350350 end
351351 end
352352 end
Original file line number Diff line number Diff line change 6262 authorized_collection . insert_one ( document )
6363 end
6464
65- it 'fails on the server when a document larger than 16MiB is inserted' do
66- document = { key : 'a' * ( max_document_size - 27 ) , _id : 'foo' }
67- expect ( document . to_bson . length ) . to eq ( max_document_size +1 )
65+ context 'on server versions >= 3.6' do
66+ min_server_fcv '3.6'
6867
69- lambda do
70- authorized_collection . insert_one ( document )
71- end . should raise_error ( Mongo ::Error ::OperationFailure , /object to insert too large/ )
68+ it 'fails on the driver when a document larger than 16MiB is inserted' do
69+ document = { key : 'a' * ( max_document_size - 27 ) , _id : 'foo' }
70+ expect ( document . to_bson . length ) . to eq ( max_document_size +1 )
71+
72+ lambda do
73+ authorized_collection . insert_one ( document )
74+ end . should raise_error ( Mongo ::Error ::MaxBSONSize , /The document exceeds maximum allowed BSON object size after serialization/ )
75+ end
76+ end
77+
78+ context 'on server versions <= 3.4' do
79+ max_server_fcv '3.4'
80+
81+ it 'fails on the server when a document larger than 16MiB is inserted' do
82+ document = { key : 'a' * ( max_document_size - 27 ) , _id : 'foo' }
83+ expect ( document . to_bson . length ) . to eq ( max_document_size +1 )
84+
85+ lambda do
86+ authorized_collection . insert_one ( document )
87+ end . should raise_error ( Mongo ::Error ::OperationFailure , /object to insert too large/ )
88+ end
7289 end
7390
7491 it 'fails in the driver when a document larger than 16MiB+16KiB is inserted' do
You can’t perform that action at this time.
0 commit comments