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 @@ -140,6 +140,8 @@ def payload
140
140
#
141
141
# @since 2.5.0
142
142
def serialize ( buffer = BSON ::ByteBuffer . new , max_bson_size = nil )
143
+ validate_document_size! ( max_bson_size )
144
+
143
145
super
144
146
add_check_sum ( buffer )
145
147
buffer
@@ -268,6 +270,23 @@ def bulk_write?
268
270
269
271
private
270
272
273
+ # Validate that the documents in this message are all smaller than the
274
+ # maxBsonObjectSize. If not, raise an exception.
275
+ def validate_document_size! ( max_bson_size )
276
+ max_bson_size ||= Mongo ::Server ::ConnectionBase ::DEFAULT_MAX_BSON_OBJECT_SIZE
277
+
278
+ contains_too_large_document = @sections . any? do |section |
279
+ section [ :type ] == 1 &&
280
+ section [ :payload ] [ :sequence ] . any? do |document |
281
+ document . to_bson . length > max_bson_size
282
+ end
283
+ end
284
+
285
+ if contains_too_large_document
286
+ raise Error ::MaxBSONSize . new ( 'The document exceeds maximum allowed BSON object size after serialization' )
287
+ end
288
+ end
289
+
271
290
def command
272
291
@command ||= if @main_document
273
292
@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