[CSHARP-1080] unexpected change non id field name in BSON document Created: 01/Oct/14  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Serialization
Affects Version/s: 1.9.2, 2.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Anatoliy Koperin Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I want to serialize following class:

public class TestModel_CustomNotId
{
    [BsonElement("id")]
    public ObjectId Id { get; set; }
}

I expected Id field to be serialized as "id",
but the result BSON document is containing "_id" field.

fix example:
https://github.com/ExM/mongo-csharp-driver/commit/264d2d912d73ddad5ebb9edc91f765a03f33e450



 Comments   
Comment by Craig Wilson [ 04/Apr/16 ]

Hi Anatoliy,

I understand where you are coming from. This certainly seems like a bug to me. However, it's not just as simple as adding release notes. I know from experience that users don't read those. Hence, this isn't an easy fix for us. I'm going to place this in the Planned but not Scheduled bucket and see if we can do something about it on our next major release (3.0). Until then, I think we are stuck.

Craig

Comment by Anatoliy Koperin [ 07/Oct/14 ]

In my case I wanted to insert a document in a database:

{
	_id : ObjectId("542d4172e4eec124d808d626"),
	optionalField : 
	{
		id : ObjectId("542d4172e4eec124d808d629")
	}
}

For mapping I used classes:

public class Demo
{
	[BsonId]
	public ObjectId Id { get; set; }
 
	[BsonElement("optionalField")]
	public OptionalField Option { get; set; }
}
 
public class OptionalField
{
	[BsonElement("id")]
	public ObjectId Id { get; set; }
}

but a document appeared in the database was:

{
	_id : ObjectId("542d4172e4eec124d808d626"),
	optionalField : 
	{
		_id : ObjectId("542d4172e4eec124d808d629")
	}
}

Such naming is unexpected to me. I found it because of the fact that the index which was expected on "optionalField.id" field did not appear.

And here are some thoughts about the backward compability:
1) If this property were used only in C# code, developers could not know what exactly is stored in collection.
2) In such case their documents in databse are already distorted
3) Driver version change is not just like "replace dll and forget", i expect developers at least to read release notes

During migration to the new version developers must add

[BsonElement("_id")]

for all fields and properties named "Id", "id" and "_id", it doesn't seems to difficult to me.

Comment by Craig Wilson [ 06/Oct/14 ]

I'm not sure what you mean. I'm not implying we break code. I'm saying that the easier thing to do here is to simply not "Find" and id member that has been marked as something other than an id. In other words, if a property called "Id" has a BsonElement("somethingElse") attribute on it, then it is obviously not the id member, so we shouldn't find it.

This is going to be backwards breaking for some people, so the trick is to figure out whether people have done this and are living with it or if it is a legitimate bug.

Comment by Anatoliy Koperin [ 06/Oct/14 ]

I think that as long as the code contains a method "public string FindIdMember (Type type)", it should work.
This method can only be removed when changing the major revision.

Comment by Craig Wilson [ 06/Oct/14 ]

Thanks for the PR: https://github.com/mongodb/mongo-csharp-driver/pull/190.

This is a very interesting problem and one that doesn't affect a high majority of people. I think the better solution would be to fix the NamedIdMemberConvention to simply not map a property like this as the id member. See here: https://github.com/mongodb/mongo-csharp-driver/blob/v1.x/MongoDB.Bson/Serialization/Conventions/NamedIdMemberConvention.cs#L121.

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