[CSHARP-2009] Discriminator incorrect when using "OfType<>" Created: 27/Jun/17  Updated: 28/Oct/23  Resolved: 02/Feb/21

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.4.4
Fix Version/s: 2.12.0

Type: Bug Priority: Major - P3
Reporter: Martin Lobger Assignee: Boris Dogadov
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File Program.cs    
Issue Links:
Related
related to CSHARP-3350 Improve types handling Closed

 Description   

When using OfType<TDerived> on a collection, the "ofTypeFilter" filter of the returned IFilteredMongoCollection<TDerivedDocument> does not generate a correct discriminator.

Consider this:

[BsonDiscriminator(RootClass = true)]
class A {}
class B : A {}

var collection = database.GetCollection<A>("users").OfType<B>();

The "collection" variable will be of type IFilteredMongoCollection<B> with an internal "ofTypeFilter" generating a discriminator looking like this:

{ "_t" : "B" }

where it should have looked like this:

{ "_t" : ["A", "B"] }

Ultimately this leads to a problem when using "upsert". Since parts of the document inserted comes from the filter, the discriminator does so as well. Since it is incorrect rendered, it leads to document in mongo with a wrong discriminator.

Attached is a small program that reveals the problem.



 Comments   
Comment by Githook User [ 02/Feb/21 ]

Author:

{'name': 'Boris', 'email': 'boris.dogadov@mongodb.com', 'username': 'BorisDog'}

Message: CSHARP-2009: Inserting correct discriminator on Upsert in OfType<> collections.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/2de35a4c9e6f5bcb76ec1bc63a242c4333c4d276

Comment by Martin Lobger [ 29/Jun/17 ]

Well, after some deeper digging into this issue, I have realized that fixing the filter is not the way to go.

I have updated the pull request #284, so that it targets the actual upsert issue more specific.

Comment by Martin Lobger [ 27/Jun/17 ]

Attempt to fix in pull request #284

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