[CSHARP-742] exposing _T makes duplicates Created: 15/May/13  Updated: 20/May/19  Resolved: 20/May/19

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

Type: Improvement Priority: Major - P3
Reporter: Allan Zimmermann Assignee: Unassigned
Resolution: Won't Fix Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

VB/C# framework 4.0 / VS 2012



 Description   

If you decorate a property to expose _t, the driver bugs out and create 2 fields in all new documents with the same name of _t ...



 Comments   
Comment by Ian Whalen (Inactive) [ 20/May/19 ]

Given the lack of activity/interest expressed by the community we're not planning on implementing this.  Closing as Won't Fix.

Comment by Craig Wilson [ 16/May/13 ]

I concur with the feature request. It would be nice for users to be able to specify a particular member as the discriminated value rather than a made element. Save storage space and enable some reuse. Users could obviously mess this up by having two difference instances return the same discriminator value, but I think it is worth the feature to enable this.

Comment by Robert Stam [ 15/May/13 ]

This should probably be reclassified as a feature request.

The discriminator has been considered a private element used by the driver to figure out the actual type of the document during deserialization. It was not anticipated that the discriminator would also be exposed as a regular property of the class.

There are two possible courses of actions here:

1. Prohibit mapping a property to the element name "_t", thus protecting the private nature of the discriminator
2. Make it possible to expose the discriminator safely

There are probably some edge cases with the second option (for example, how to prevent the application from writing invalid values to the discriminator?).

Comment by Robert Stam [ 15/May/13 ]

Note: Edited from original version.

To reproduce use these classes:

public class B
{
    public int Id { get; set; }
    [BsonElement("_t")]
    public string Discriminator { get; set; }
}
 
public class C : B
{
}

and this code:

var collection = database.GetCollection<B>("test");
var c = new C { Id = 1, Discriminator = "Bogus" };
collection.Insert(c);
 
var document = collection.FindOneAs<BsonDocument>();
Console.WriteLine(document.ToJson());

and the output is:

{ "_id" : 1, "_t" : "C", "_t" : "Bogus" }

If you use the shell to check the document the result is a little odd because the shell doesn't really support duplicate elements:

> db.test.find()
{ "_id" : 1, "_t" : "C", "_t" : "C" }
>

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