[CSHARP-180] Misleading exception message Created: 11/Mar/11  Updated: 02/Apr/15  Resolved: 15/Mar/11

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

Type: Bug Priority: Minor - P4
Reporter: Eric Z. Beard Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7, latest code from github



 Description   

If you get an error like this:

MongoDB.Bson.BsonSerializationException: Duplicate element name 'EntryId' in class 'LoopLib.Common.LogEntry'

It might not have nothing to do with "EntryId". It's due to a duplicate BsonElement name anywhere in the
class, not necessarily on that field.

For example, I had something like this:

[BsonId]
public Guid EntryId

{ get; set; }

[BsonElement("a")]
public int A { get; set; }

[BsonElement("a")]
public int B

{ get; set; }

For people who are renaming their fields to save storage space, this will be a common mistake.



 Comments   
Comment by Robert Stam [ 15/Mar/11 ]

Improved the error message.

For example, the following class:

public class C

{ public ObjectId Id; [BsonElement("a")] public int A; [BsonElement("a")] public int B; }

Now results in this error message:

Member 'B' of class 'C' cannot use element name 'a' because it is already being used by member 'A'.

Comment by Sridhar Nanjundeswaran [ 11/Mar/11 ]

Cannot reproduce the issue of the confusing error message with either the latest C# driver or the C# driver v0.11.0.4042. The error message we got with the with the following code was
"Duplicate element name 'B' in class 'ConsoleApplication1.Foo'".
namespace ConsoleApplication1
{
class Foo
{
[BsonId]
public Guid EntryId

{ get; set; }

[BsonElement("a")]
public int A { get; set; }

[BsonElement("a")]
public int B

{ get; set; }


}

class Program
{
static void Main(string[] args)
{
var foo = new Foo()

{ A = 1, B = 2 }

;
var json = foo.ToJson();
}
}
}

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