Skip to content

Commit ac83d02

Browse files
committed
CSHARP-2276: IMongoIndexManager CreateOne or CreateOneAsync methods can throw NullReferenceException
1 parent 281c2af commit ac83d02

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/MongoDB.Driver/MongoIndexManagerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public virtual Task DropOneAsync(IClientSessionHandle session, string name, Drop
292292

293293
private CreateManyIndexesOptions ToCreateManyIndexesOptions(CreateOneIndexOptions options)
294294
{
295-
return new CreateManyIndexesOptions { MaxTime = options.MaxTime };
295+
return new CreateManyIndexesOptions { MaxTime = options?.MaxTime };
296296
}
297297
}
298298
}

tests/MongoDB.Driver.Tests/MongoCollectionImplTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,7 @@ public void FindOneAndUpdate_with_Projection_As_should_execute_correctly(
14081408
[ParameterAttributeData]
14091409
public void Indexes_CreateOne_should_execute_a_CreateIndexesOperation(
14101410
[Values(false, true)] bool usingSession,
1411+
[Values(false, true)] bool usingCreateOneIndexOptions,
14111412
[Values(false, true)] bool async,
14121413
[Values(null, -1, 0, 42, 9000)] int? milliseconds)
14131414
{
@@ -1421,7 +1422,7 @@ public void Indexes_CreateOne_should_execute_a_CreateIndexesOperation(
14211422
var weights = new BsonDocument("y", 1);
14221423
var storageEngine = new BsonDocument("awesome", true);
14231424
var maxTime = milliseconds != null ? TimeSpan.FromMilliseconds(milliseconds.Value) : (TimeSpan?)null;
1424-
var createOneIndexOptions = new CreateOneIndexOptions { MaxTime = maxTime };
1425+
var createOneIndexOptions = usingCreateOneIndexOptions ? new CreateOneIndexOptions { MaxTime = maxTime } : null;
14251426
var options = new CreateIndexOptions<BsonDocument>
14261427
{
14271428
Background = true,
@@ -1474,7 +1475,7 @@ public void Indexes_CreateOne_should_execute_a_CreateIndexesOperation(
14741475

14751476
var operation = call.Operation.Should().BeOfType<CreateIndexesOperation>().Subject;
14761477
operation.CollectionNamespace.FullName.Should().Be("foo.bar");
1477-
operation.MaxTime.Should().Be(createOneIndexOptions.MaxTime);
1478+
operation.MaxTime.Should().Be(createOneIndexOptions?.MaxTime);
14781479
operation.Requests.Count().Should().Be(1);
14791480
operation.WriteConcern.Should().BeSameAs(writeConcern);
14801481

@@ -1506,6 +1507,7 @@ public void Indexes_CreateOne_should_execute_a_CreateIndexesOperation(
15061507
[ParameterAttributeData]
15071508
public void Indexes_CreateMany_should_execute_a_CreateIndexesOperation(
15081509
[Values(false, true)] bool usingSession,
1510+
[Values(false, true)] bool usingCreateManyIndexesOptions,
15091511
[Values(false, true)] bool async,
15101512
[Values(null, -1, 0, 42, 9000)] int? milliseconds)
15111513
{
@@ -1521,7 +1523,7 @@ public void Indexes_CreateMany_should_execute_a_CreateIndexesOperation(
15211523
var weights = new BsonDocument("y", 1);
15221524
var storageEngine = new BsonDocument("awesome", true);
15231525
var maxTime = milliseconds != null ? TimeSpan.FromMilliseconds(milliseconds.Value) : (TimeSpan?)null;
1524-
var createManyIndexesOptions = new CreateManyIndexesOptions { MaxTime = maxTime };
1526+
var createManyIndexesOptions = usingCreateManyIndexesOptions ? new CreateManyIndexesOptions { MaxTime = maxTime } : null;
15251527

15261528
var options = new CreateIndexOptions<BsonDocument>
15271529
{
@@ -1576,7 +1578,7 @@ public void Indexes_CreateMany_should_execute_a_CreateIndexesOperation(
15761578

15771579
var operation = call.Operation.Should().BeOfType<CreateIndexesOperation>().Subject;
15781580
operation.CollectionNamespace.Should().Be(subject.CollectionNamespace);
1579-
operation.MaxTime.Should().Be(createManyIndexesOptions.MaxTime);
1581+
operation.MaxTime.Should().Be(createManyIndexesOptions?.MaxTime);
15801582
operation.Requests.Count().Should().Be(2);
15811583
operation.WriteConcern.Should().BeSameAs(writeConcern);
15821584

0 commit comments

Comments
 (0)