[CSHARP-958] Insert is not properly assigning the _id values when the nominal type is an interface Created: 21/Apr/14  Updated: 02/Apr/16  Resolved: 21/Apr/14

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 1.9
Fix Version/s: 1.9.1

Type: Bug Priority: Major - P3
Reporter: Roman Bäriswyl [X] Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 8.1



 Description   

When using Interfaces for class-definitions for documents and then using collection.Insert with this interface, ObjectId elements are always ObjectId.Empty. This does work correctly with version 1.8.3 and now broke with 1.9.0.

Here is a very simple sample with the Person class which implements the IPerson interface. When using the IPerson interface in collection.Insert, the Id is always ObjectId.Empty, when casting the newPerson variable back to Person before inserting, it works correctly. When using c#-driver 1.8.3 (maybe also lower ones), it works with IPerson or casting to Person.

using MongoDB.Bson;
using MongoDB.Driver;
 
namespace Mongo19Test
{
    public interface IPerson { }
 
    public class Person : IPerson
    {
        public ObjectId Id { get; set; }
        public string Name { get; set; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var client = new MongoClient("mongodb://localhost:27017");
            var server = client.GetServer();
            var database = server.GetDatabase("foo");
            var collection = database.GetCollection<Person>("bar");
            IPerson newPerson = new Person { Name = "Jack" };
            collection.Insert(newPerson);
        }
    }
}



 Comments   
Comment by Githook User [ 23/Apr/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-958: Fix AssignId when nominal type is an interface.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/3453d51f39862e304aaeada8cef5bc89cea5136f

Comment by Githook User [ 21/Apr/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-958: Fix AssignId when nominal type is an interface.
Branch: v1.x
https://github.com/mongodb/mongo-csharp-driver/commit/9ea5ee2c2d3969fd2ea1bbea92e1157519bf8d2f

Comment by Robert Stam [ 21/Apr/14 ]

Thanks for the report. I am able to reproduce it and am looking into it.

Generated at Wed Feb 07 21:38:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.