Skip to content

Commit 5129fc9

Browse files
committed
CSHARP-1427: Added support for BypassDocumentValidation (2.x Legacy API).
1 parent 0bc63d6 commit 5129fc9

File tree

12 files changed

+141
-2
lines changed

12 files changed

+141
-2
lines changed

src/MongoDB.Driver.Core/Core/Operations/InsertOpcodeOperation.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace MongoDB.Driver.Core.Operations
3636
public class InsertOpcodeOperation<TDocument> : IWriteOperation<IEnumerable<WriteConcernResult>>
3737
{
3838
// fields
39+
private bool? _bypassDocumentValidation;
3940
private readonly CollectionNamespace _collectionNamespace;
4041
private bool _continueOnError;
4142
private readonly BatchableSource<TDocument> _documentSource;
@@ -64,6 +65,18 @@ public InsertOpcodeOperation(CollectionNamespace collectionNamespace, BatchableS
6465
}
6566

6667
// properties
68+
/// <summary>
69+
/// Gets or sets a value indicating whether to bypass document validation.
70+
/// </summary>
71+
/// <value>
72+
/// A value indicating whether to bypass document validation.
73+
/// </value>
74+
public bool? BypassDocumentValidation
75+
{
76+
get { return _bypassDocumentValidation; }
77+
set { _bypassDocumentValidation = value; }
78+
}
79+
6780
/// <summary>
6881
/// Gets the collection namespace.
6982
/// </summary>
@@ -234,6 +247,7 @@ private InsertOpcodeOperationEmulator<TDocument> CreateEmulator()
234247
{
235248
return new InsertOpcodeOperationEmulator<TDocument>(_collectionNamespace, _serializer, _documentSource, _messageEncoderSettings)
236249
{
250+
BypassDocumentValidation = _bypassDocumentValidation,
237251
ContinueOnError = _continueOnError,
238252
MaxBatchCount = _maxBatchCount,
239253
MaxDocumentSize = _maxDocumentSize,

src/MongoDB.Driver.Core/Core/Operations/InsertOpcodeOperationEmulator.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace MongoDB.Driver.Core.Operations
2929
internal class InsertOpcodeOperationEmulator<TDocument>
3030
{
3131
// fields
32+
private bool? _bypassDocumentValidation;
3233
private readonly CollectionNamespace _collectionNamespace;
3334
private bool _continueOnError;
3435
private readonly BatchableSource<TDocument> _documentSource;
@@ -53,6 +54,12 @@ public InsertOpcodeOperationEmulator(
5354
}
5455

5556
// properties
57+
public bool? BypassDocumentValidation
58+
{
59+
get { return _bypassDocumentValidation; }
60+
set { _bypassDocumentValidation = value; }
61+
}
62+
5663
public CollectionNamespace CollectionNamespace
5764
{
5865
get { return _collectionNamespace; }
@@ -158,6 +165,7 @@ private BulkInsertOperation CreateOperation()
158165
});
159166
return new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings)
160167
{
168+
BypassDocumentValidation = _bypassDocumentValidation,
161169
IsOrdered = !_continueOnError,
162170
MaxBatchCount = _maxBatchCount,
163171
MaxBatchLength = _maxMessageSize,

src/MongoDB.Driver.Core/Core/Operations/UpdateOpcodeOperation.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace MongoDB.Driver.Core.Operations
3232
public class UpdateOpcodeOperation : IWriteOperation<WriteConcernResult>
3333
{
3434
// fields
35+
private bool? _bypassDocumentValidation;
3536
private readonly CollectionNamespace _collectionNamespace;
3637
private int? _maxDocumentSize;
3738
private readonly MessageEncoderSettings _messageEncoderSettings;
@@ -56,6 +57,18 @@ public UpdateOpcodeOperation(
5657
}
5758

5859
// properties
60+
/// <summary>
61+
/// Gets or sets a value indicating whether to bypass document validation.
62+
/// </summary>
63+
/// <value>
64+
/// A value indicating whether to bypass document validation.
65+
/// </value>
66+
public bool? BypassDocumentValidation
67+
{
68+
get { return _bypassDocumentValidation; }
69+
set { _bypassDocumentValidation = value; }
70+
}
71+
5972
/// <summary>
6073
/// Gets the collection namespace.
6174
/// </summary>
@@ -161,6 +174,7 @@ private UpdateOpcodeOperationEmulator CreateEmulator()
161174
{
162175
return new UpdateOpcodeOperationEmulator(_collectionNamespace, _request, _messageEncoderSettings)
163176
{
177+
BypassDocumentValidation = _bypassDocumentValidation,
164178
MaxDocumentSize = _maxDocumentSize,
165179
WriteConcern = _writeConcern
166180
};

src/MongoDB.Driver.Core/Core/Operations/UpdateOpcodeOperationEmulator.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace MongoDB.Driver.Core.Operations
2626
internal class UpdateOpcodeOperationEmulator
2727
{
2828
// fields
29+
private bool? _bypassDocumentValidation;
2930
private readonly CollectionNamespace _collectionNamespace;
3031
private int? _maxDocumentSize;
3132
private readonly MessageEncoderSettings _messageEncoderSettings;
@@ -44,6 +45,12 @@ public UpdateOpcodeOperationEmulator(
4445
}
4546

4647
// properties
48+
public bool? BypassDocumentValidation
49+
{
50+
get { return _bypassDocumentValidation; }
51+
set { _bypassDocumentValidation = value; }
52+
}
53+
4754
public CollectionNamespace CollectionNamespace
4855
{
4956
get { return _collectionNamespace; }
@@ -124,6 +131,7 @@ private BulkUpdateOperation CreateOperation()
124131
var requests = new[] { _request };
125132
return new BulkUpdateOperation(_collectionNamespace, requests, _messageEncoderSettings)
126133
{
134+
BypassDocumentValidation = _bypassDocumentValidation,
127135
IsOrdered = true,
128136
WriteConcern = _writeConcern
129137
};

src/MongoDB.Driver.Legacy.Tests/MongoCollectionTests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ public void TestAggregateOutputToCollection()
205205

206206
var query = _collection.Aggregate(new AggregateArgs
207207
{
208+
BypassDocumentValidation = true,
208209
Pipeline = new BsonDocument[]
209210
{
210211
new BsonDocument("$group", new BsonDocument { { "_id", "$x" }, { "count", new BsonDocument("$sum", 1) } }),
@@ -873,6 +874,7 @@ public void TestFindAndModify()
873874
started = started.AddTicks(-(started.Ticks % 10000)); // adjust for MongoDB DateTime precision
874875
var args = new FindAndModifyArgs
875876
{
877+
BypassDocumentValidation = true,
876878
Query = Query.EQ("inprogress", false),
877879
SortBy = SortBy.Descending("priority"),
878880
Update = Update.Set("inprogress", true).Set("started", started),
@@ -2143,7 +2145,11 @@ public void TestInsertBatchContinueOnError()
21432145
document.Remove("_id");
21442146
}
21452147

2146-
var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
2148+
var options = new MongoInsertOptions
2149+
{
2150+
BypassDocumentValidation = true,
2151+
Flags = InsertFlags.ContinueOnError
2152+
};
21472153
exception = Assert.Throws<MongoDuplicateKeyException>(() => collection.InsertBatch(batch, options));
21482154
result = exception.WriteConcernResult;
21492155

@@ -2526,6 +2532,7 @@ public void TestMapReduce()
25262532

25272533
var result = _collection.MapReduce(new MapReduceArgs
25282534
{
2535+
BypassDocumentValidation = true,
25292536
MapFunction = map,
25302537
ReduceFunction = reduce,
25312538
OutputMode = MapReduceOutputMode.Replace,
@@ -3043,7 +3050,9 @@ public void TestUpdate()
30433050
{
30443051
_collection.Drop();
30453052
_collection.Insert(new BsonDocument("x", 1));
3046-
var result = _collection.Update(Query.EQ("x", 1), Update.Set("x", 2));
3053+
var options = new MongoUpdateOptions { BypassDocumentValidation = true };
3054+
3055+
var result = _collection.Update(Query.EQ("x", 1), Update.Set("x", 2), options);
30473056

30483057
var expectedResult = new ExpectedWriteConcernResult
30493058
{

src/MongoDB.Driver.Legacy/AggregateArgs.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class AggregateArgs
4343
// private fields
4444
private bool? _allowDiskUse;
4545
private int? _batchSize;
46+
private bool? _bypassDocumentValidation;
4647
private TimeSpan? _maxTime;
4748
private AggregateOutputMode _outputMode = AggregateOutputMode.Inline;
4849
private IEnumerable<BsonDocument> _pipeline;
@@ -73,6 +74,18 @@ public int? BatchSize
7374
}
7475
}
7576

77+
/// <summary>
78+
/// Gets or sets a value indicating whether to bypass document validation.
79+
/// </summary>
80+
/// <value>
81+
/// A value indicating whether to bypass document validation.
82+
/// </value>
83+
public bool? BypassDocumentValidation
84+
{
85+
get { return _bypassDocumentValidation; }
86+
set { _bypassDocumentValidation = value; }
87+
}
88+
7689
/// <summary>
7790
/// Gets or sets the max time the server should spend on the aggregation command.
7891
/// </summary>

src/MongoDB.Driver.Legacy/FindAndModifyArgs.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public enum FindAndModifyDocumentVersion
3838
public class FindAndModifyArgs
3939
{
4040
// private fields
41+
private bool? _bypassDocumentValidation;
4142
private IMongoFields _fields;
4243
private TimeSpan? _maxTime;
4344
private IMongoQuery _query;
@@ -47,6 +48,18 @@ public class FindAndModifyArgs
4748
private FindAndModifyDocumentVersion? _versionReturned;
4849

4950
// public properties
51+
/// <summary>
52+
/// Gets or sets a value indicating whether to bypass document validation.
53+
/// </summary>
54+
/// <value>
55+
/// A value indicating whether to bypass document validation.
56+
/// </value>
57+
public bool? BypassDocumentValidation
58+
{
59+
get { return _bypassDocumentValidation; }
60+
set { _bypassDocumentValidation = value; }
61+
}
62+
5063
/// <summary>
5164
/// Gets or sets the fields specification.
5265
/// </summary>

src/MongoDB.Driver.Legacy/MapReduceArgs.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public static Core.Operations.MapReduceOutputMode ToCore(this MapReduceOutputMod
6666
public class MapReduceArgs
6767
{
6868
// private fields
69+
private bool? _bypassDocumentValidation;
6970
private BsonJavaScript _finalizeFunction;
7071
private bool? _jsMode;
7172
private long? _limit;
@@ -83,6 +84,18 @@ public class MapReduceArgs
8384
private bool? _verbose;
8485

8586
// public properties
87+
/// <summary>
88+
/// Gets or sets a value indicating whether to bypass document validation.
89+
/// </summary>
90+
/// <value>
91+
/// A value indicating whether to bypass document validation.
92+
/// </value>
93+
public bool? BypassDocumentValidation
94+
{
95+
get { return _bypassDocumentValidation; }
96+
set { _bypassDocumentValidation = value; }
97+
}
98+
8699
/// <summary>
87100
/// Gets or sets the finalize function.
88101
/// </summary>

src/MongoDB.Driver.Legacy/MongoCollection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public virtual IEnumerable<BsonDocument> Aggregate(AggregateArgs args)
135135
var aggregateOperation = new AggregateToCollectionOperation(_collectionNamespace, args.Pipeline, messageEncoderSettings)
136136
{
137137
AllowDiskUse = args.AllowDiskUse,
138+
BypassDocumentValidation = args.BypassDocumentValidation,
138139
MaxTime = args.MaxTime
139140
};
140141
ExecuteWriteOperation(aggregateOperation);
@@ -567,6 +568,7 @@ public virtual FindAndModifyResult FindAndModify(FindAndModifyArgs args)
567568
{
568569
operation = new FindOneAndUpdateOperation<BsonDocument>(_collectionNamespace, filter, updateDocument, resultSerializer, messageEncoderSettings)
569570
{
571+
BypassDocumentValidation = args.BypassDocumentValidation,
570572
IsUpsert = args.Upsert,
571573
MaxTime = args.MaxTime,
572574
Projection = projection,
@@ -580,6 +582,7 @@ public virtual FindAndModifyResult FindAndModify(FindAndModifyArgs args)
580582
var replacement = updateDocument;
581583
operation = new FindOneAndReplaceOperation<BsonDocument>(_collectionNamespace, filter, replacement, resultSerializer, messageEncoderSettings)
582584
{
585+
BypassDocumentValidation = args.BypassDocumentValidation,
583586
IsUpsert = args.Upsert,
584587
MaxTime = args.MaxTime,
585588
Projection = projection,
@@ -1405,6 +1408,7 @@ public virtual IEnumerable<WriteConcernResult> InsertBatch<TNominalType>(
14051408

14061409
var operation = new InsertOpcodeOperation<TNominalType>(_collectionNamespace, documentSource, serializer, messageEncoderSettings)
14071410
{
1411+
BypassDocumentValidation = options.BypassDocumentValidation,
14081412
ContinueOnError = continueOnError,
14091413
WriteConcern = writeConcern
14101414
};
@@ -1543,6 +1547,7 @@ public virtual MapReduceResult MapReduce(MapReduceArgs args)
15431547
args.ReduceFunction,
15441548
messageEncoderSettings)
15451549
{
1550+
BypassDocumentValidation = args.BypassDocumentValidation,
15461551
Filter = query,
15471552
FinalizeFunction = args.FinalizeFunction,
15481553
JavaScriptMode = args.JsMode,
@@ -1905,6 +1910,7 @@ public virtual WriteConcernResult Update(IMongoQuery query, IMongoUpdate update,
19051910
};
19061911
var operation = new UpdateOpcodeOperation(_collectionNamespace, request, messageEncoderSettings)
19071912
{
1913+
BypassDocumentValidation = options.BypassDocumentValidation,
19081914
WriteConcern = writeConcern
19091915
};
19101916

src/MongoDB.Driver.Legacy/MongoInsertOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace MongoDB.Driver
2323
public class MongoInsertOptions
2424
{
2525
// private fields
26+
private bool? _bypassDocumentValidation;
2627
private InsertFlags _flags;
2728
private WriteConcern _writeConcern;
2829

@@ -46,6 +47,18 @@ public MongoInsertOptions(MongoCollection collection) : this()
4647
}
4748

4849
// public properties
50+
/// <summary>
51+
/// Gets or sets a value indicating whether to bypass document validation.
52+
/// </summary>
53+
/// <value>
54+
/// A value indicating whether to bypass document validation.
55+
/// </value>
56+
public bool? BypassDocumentValidation
57+
{
58+
get { return _bypassDocumentValidation; }
59+
set { _bypassDocumentValidation = value; }
60+
}
61+
4962
/// <summary>
5063
/// Gets or sets the insert flags.
5164
/// </summary>

src/MongoDB.Driver.Legacy/MongoUpdateOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace MongoDB.Driver
2323
public class MongoUpdateOptions
2424
{
2525
// private fields
26+
private bool? _bypassDocumentValidation;
2627
private UpdateFlags _flags;
2728
private WriteConcern _writeConcern;
2829

@@ -46,6 +47,18 @@ public MongoUpdateOptions(MongoCollection collection) : this()
4647
}
4748

4849
// public properties
50+
/// <summary>
51+
/// Gets or sets a value indicating whether to bypass document validation.
52+
/// </summary>
53+
/// <value>
54+
/// A value indicating whether to bypass document validation.
55+
/// </value>
56+
public bool? BypassDocumentValidation
57+
{
58+
get { return _bypassDocumentValidation; }
59+
set { _bypassDocumentValidation = value; }
60+
}
61+
4962
/// <summary>
5063
/// Gets or sets the update flags.
5164
/// </summary>

src/MongoDB.Driver.Legacy/Operations/BulkWriteOperation.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace MongoDB.Driver
3232
public sealed class BulkWriteOperation<TDocument>
3333
{
3434
// private fields
35+
private bool? _bypassDocumentValidation;
3536
private readonly MongoCollection _collection;
3637
private readonly bool _isOrdered;
3738
private readonly List<WriteRequest> _requests = new List<WriteRequest>();
@@ -44,6 +45,19 @@ internal BulkWriteOperation(MongoCollection collection, bool isOrdered)
4445
_isOrdered = isOrdered;
4546
}
4647

48+
// public properties
49+
/// <summary>
50+
/// Gets or sets a value indicating whether to bypass document validation.
51+
/// </summary>
52+
/// <value>
53+
/// A value indicating whether to bypass document validation.
54+
/// </value>
55+
public bool? BypassDocumentValidation
56+
{
57+
get { return _bypassDocumentValidation; }
58+
set { _bypassDocumentValidation = value; }
59+
}
60+
4761
// public methods
4862
/// <summary>
4963
/// Executes the bulk operation using the default write concern from the collection.
@@ -151,6 +165,7 @@ private BulkWriteResult<TDocument> ExecuteHelper(WriteConcern writeConcern)
151165

152166
var operation = new BulkMixedWriteOperation(new CollectionNamespace(_collection.Database.Name, _collection.Name), requests, messageEncoderSettings)
153167
{
168+
BypassDocumentValidation = _bypassDocumentValidation,
154169
IsOrdered = _isOrdered,
155170
WriteConcern = writeConcern
156171
};

0 commit comments

Comments
 (0)