File tree 3 files changed +45
-9
lines changed
3 files changed +45
-9
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,8 @@ def payload
147
147
#
148
148
# @since 2.5.0
149
149
def serialize ( buffer = BSON ::ByteBuffer . new , max_bson_size = nil )
150
+ validate_document_size! ( max_bson_size )
151
+
150
152
super
151
153
add_check_sum ( buffer )
152
154
buffer
@@ -275,6 +277,23 @@ def bulk_write?
275
277
276
278
private
277
279
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
+
278
297
def command
279
298
@command ||= if @main_document
280
299
@main_document . dup . tap do |cmd |
Original file line number Diff line number Diff line change 113
113
it 'raises an exception' do
114
114
expect do
115
115
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 / )
117
117
end
118
118
end
119
119
end
255
255
it 'raises an exception' do
256
256
expect do
257
257
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 / )
259
259
end
260
260
end
261
261
end
346
346
it 'raises an exception' do
347
347
expect do
348
348
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 / )
350
350
end
351
351
end
352
352
end
Original file line number Diff line number Diff line change 62
62
authorized_collection . insert_one ( document )
63
63
end
64
64
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'
68
67
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
72
89
end
73
90
74
91
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