Skip to content

Commit a5ac07a

Browse files
committed
CSHARP-1198: fixed null reference bug in FindFluent.CountAsync.
1 parent 00f30cf commit a5ac07a

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Copyright 2010-2014 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Threading;
17+
using System.Threading.Tasks;
18+
using FluentAssertions;
19+
using MongoDB.Bson;
20+
using NSubstitute;
21+
using NUnit.Framework;
22+
23+
namespace MongoDB.Driver.Tests
24+
{
25+
[TestFixture]
26+
public class FindFluentTests
27+
{
28+
private IMongoCollection<Person> _collection;
29+
30+
[Test]
31+
public void CountAsync_should_not_throw_a_null_reference_exception()
32+
{
33+
var subject = CreateSubject();
34+
35+
var result = subject.CountAsync().GetAwaiter().GetResult();
36+
37+
_collection.Received().CountAsync(
38+
subject.Filter,
39+
Arg.Any<CountOptions>(),
40+
Arg.Any<CancellationToken>());
41+
}
42+
43+
private IFindFluent<Person, Person> CreateSubject()
44+
{
45+
var settings = new MongoCollectionSettings();
46+
_collection = Substitute.For<IMongoCollection<Person>>();
47+
_collection.Settings.Returns(settings);
48+
var options = new FindOptions<Person, Person>();
49+
var subject = new FindFluent<Person, Person>(_collection, new BsonDocument(), options);
50+
51+
return subject;
52+
}
53+
54+
public class Person
55+
{
56+
public string FirstName;
57+
public string LastName;
58+
public int Age;
59+
}
60+
}
61+
}

src/MongoDB.Driver.Tests/MongoDB.Driver.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
</Compile>
9494
<Compile Include="BulkWriteErrorTests.cs" />
9595
<Compile Include="FieldDefinitionTests.cs" />
96+
<Compile Include="FindFluentTests.cs" />
9697
<Compile Include="Linq\Translators\LegacyPredicateTranslatorTests.cs" />
9798
<Compile Include="Linq\Translators\TranslatorTestBase.cs" />
9899
<Compile Include="Linq\Translators\PredicateTranslatorTests.cs" />

src/MongoDB.Driver/FindFluent.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ public override FindOptions<TDocument, TProjection> Options
5050
// methods
5151
public override Task<long> CountAsync(CancellationToken cancellationToken)
5252
{
53-
BsonValue hint;
54-
_options.Modifiers.TryGetValue("$hint", out hint);
53+
BsonValue hint = null;
54+
if (_options.Modifiers != null)
55+
{
56+
_options.Modifiers.TryGetValue("$hint", out hint);
57+
}
5558
var options = new CountOptions
5659
{
5760
Hint = hint,

0 commit comments

Comments
 (0)