[CSHARP-81] Generate ObjectId to wrong property when BsonId property is in the parent class Created: 22/Oct/10  Updated: 19/Oct/16  Resolved: 22/Oct/10

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Moved from: http://github.com/mongodb/mongo-csharp-driver/issues#issue/5

Hi,

I think this is not the correct result. Please see the following code:

public class BaseModel
{ [BsonId] public ObjectId Id

{ get; set; } }

public class User : BaseModel
{ public ObjectId FriendId { get; set; }

}

[TestFixture] public class Test
{ [Test] public void New()

{ ... // create collections var model = new User(); collection.Save(model); }

}

The result in mongodb is:

db.user.find()

{ "FriendId" : ObjectId("xxxxxxxxxxxxxxxxxxxxxxx"), "_id" : ObjectId("0000000000000000000") }

[I modified above code from my real code so the result might not be the same order but the point is in my result, new object id is assigned to wrong property]



 Comments   
Comment by Payut M [ 22/Oct/10 ]

Regarding the guess: To me, I don't think guessing is good. The driver should let's user decides which one is the Id property by either using [BsonId] or specify with Mapping.

To newbie person like me, the consequence of guessing wrong will be that I will have to waste sometime finding the bug which I am not aware before.

Anyway, thank for the fixed.

Comment by Robert Stam [ 22/Oct/10 ]

Fixed bug and added a unit test for this. The Id property now comes from the highest class in the inheritance tree that is able to provide one. The algorithm for detecting an Id field is pretty basic (first field found ending in "Id" or "id" or of type ObjectId or Guid), but of course using the [BsonId] attribute makes guessing unnecessary.

I'm wondering whether we should even try to guess at the Id property. What are the consequences of guessing wrong?

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