Skip to content

Commit 9ea5ee2

Browse files
author
rstam
committed
CSHARP-958: Fix AssignId when nominal type is an interface.
1 parent 8d8b18a commit 9ea5ee2

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

MongoDB.Driver/MongoCollection.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,12 +2173,13 @@ internal AggregateResult RunAggregateCommand(AggregateArgs args)
21732173
// private methods
21742174
private void AssignId(InsertRequest request)
21752175
{
2176-
var serializer = request.Serializer ?? BsonSerializer.LookupSerializer(request.NominalType);
2177-
var idProvider = serializer as IBsonIdProvider;
2178-
if (idProvider != null)
2176+
var document = request.Document;
2177+
if (document != null)
21792178
{
2180-
var document = request.Document;
2181-
if (document != null)
2179+
var actualType = document.GetType();
2180+
var serializer = request.Serializer ?? BsonSerializer.LookupSerializer(actualType);
2181+
var idProvider = serializer as IBsonIdProvider;
2182+
if (idProvider != null)
21822183
{
21832184
object id;
21842185
Type idNominalType;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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;
17+
using System.Linq;
18+
using MongoDB.Bson;
19+
using MongoDB.Driver;
20+
using NUnit.Framework;
21+
22+
namespace MongoDB.DriverUnitTests.Jira
23+
{
24+
[TestFixture]
25+
public class CSharp958Tests
26+
{
27+
private interface IPerson { }
28+
29+
private class Person : IPerson
30+
{
31+
public ObjectId Id { get; set; }
32+
public string Name { get; set; }
33+
}
34+
35+
[Test]
36+
public void TestAssignIdWorksWithInterface()
37+
{
38+
var collection = Configuration.GetTestCollection<IPerson>();
39+
collection.Drop();
40+
41+
IPerson person = new Person { Name = "Jack" };
42+
var result = collection.Insert(person);
43+
Assert.AreNotEqual(ObjectId.Empty, ((Person)person).Id);
44+
}
45+
}
46+
}

MongoDB.DriverUnitTests/MongoDB.DriverUnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
<Compile Include="Jira\CSharp801Tests.cs" />
140140
<Compile Include="Jira\CSharp840Tests.cs" />
141141
<Compile Include="Jira\CSharp893Tests.cs" />
142+
<Compile Include="Jira\CSharp958Tests.cs" />
142143
<Compile Include="Linq\BsonDocumentBackedClassSerializerTests.cs" />
143144
<Compile Include="Linq\BsonDocumentTests.cs" />
144145
<Compile Include="FailPoint.cs" />

0 commit comments

Comments
 (0)