Skip to content

Commit 0bcca2e

Browse files
author
rstam
committed
CSHARP-801: The container parameter to GenerateId should be a MongoCollection (not an InsertOperation).
1 parent f535711 commit 0bcca2e

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

MongoDB.Driver/MongoCollection.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,8 @@ public virtual IEnumerable<WriteConcernResult> InsertBatch(
11391139
options.CheckElementNames,
11401140
nominalType,
11411141
documents,
1142-
options.Flags);
1142+
options.Flags,
1143+
this);
11431144

11441145
var connection = _server.AcquireConnection(ReadPreference.Primary);
11451146
try

MongoDB.Driver/Operations/InsertOperation.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ internal class InsertOperation : WriteOperation
2929
private readonly Type _documentType;
3030
private readonly IEnumerable _documents;
3131
private readonly InsertFlags _flags;
32+
private readonly object _idGeneratorContainer;
3233

3334
public InsertOperation(
3435
string databaseName,
@@ -40,14 +41,16 @@ public InsertOperation(
4041
bool checkElementNames,
4142
Type documentType,
4243
IEnumerable documents,
43-
InsertFlags flags)
44+
InsertFlags flags,
45+
object idGeneratorContainer)
4446
: base(databaseName, collectionName, readerSettings, writerSettings, writeConcern)
4547
{
4648
_assignIdOnInsert = assignIdOnInsert;
4749
_checkElementNames = checkElementNames;
4850
_documentType = documentType;
4951
_documents = documents;
5052
_flags = flags;
53+
_idGeneratorContainer = idGeneratorContainer;
5154
}
5255

5356
public IEnumerable<WriteConcernResult> Execute(MongoConnection connection)
@@ -85,7 +88,7 @@ public IEnumerable<WriteConcernResult> Execute(MongoConnection connection)
8588
{
8689
if (idGenerator != null && idGenerator.IsEmpty(id))
8790
{
88-
id = idGenerator.GenerateId(this, document);
91+
id = idGenerator.GenerateId(_idGeneratorContainer, document);
8992
idProvider.SetDocumentId(document, id);
9093
}
9194
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* Copyright 2010-2013 10gen 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 MongoDB.Bson.Serialization;
17+
using MongoDB.Bson.Serialization.Attributes;
18+
using MongoDB.Driver;
19+
using NUnit.Framework;
20+
21+
namespace MongoDB.DriverUnitTests.Jira
22+
{
23+
[TestFixture]
24+
public class CSharp801
25+
{
26+
private MongoCollection<C> _collection;
27+
28+
[SetUp]
29+
public void SetUp()
30+
{
31+
_collection = Configuration.GetTestCollection<C>();
32+
if (_collection.Exists()) { _collection.Drop(); }
33+
}
34+
35+
[Test]
36+
public void GenerateIdCalledFromInsert()
37+
{
38+
_collection.RemoveAll();
39+
_collection.Insert(new C());
40+
var c = _collection.FindOne();
41+
Assert.AreEqual(1, c.Id);
42+
}
43+
44+
[Test]
45+
public void GenerateIdCalledFromSave()
46+
{
47+
_collection.RemoveAll();
48+
_collection.Save(new C());
49+
var c = _collection.FindOne();
50+
Assert.AreEqual(1, c.Id);
51+
}
52+
53+
// nested classes
54+
public class C
55+
{
56+
[BsonId(IdGenerator = typeof(MyIdGenerator))]
57+
public int Id { get; set; }
58+
}
59+
60+
public class MyIdGenerator : IIdGenerator
61+
{
62+
public object GenerateId(object container, object document)
63+
{
64+
var collection = (MongoCollection<C>)container; // should not throw an InvalidCastException
65+
return 1;
66+
}
67+
68+
public bool IsEmpty(object id)
69+
{
70+
return (int)id == 0;
71+
}
72+
}
73+
}
74+
}

MongoDB.DriverUnitTests/MongoDB.DriverUnitTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<Compile Include="Jira\CSharp714Tests.cs" />
123123
<Compile Include="Jira\CSharp718Tests.cs" />
124124
<Compile Include="Jira\CSharp779Tests.cs" />
125+
<Compile Include="Jira\CSharp801Tests.cs" />
125126
<Compile Include="MongoClientSettingsTests.cs" />
126127
<Compile Include="ReadPreferenceTests.cs" />
127128
<Compile Include="MongoCollectionSettingsTests.cs" />

0 commit comments

Comments
 (0)