[CSHARP-3154] Can't register DiscriminatorConvention to object type Created: 07/Jul/20  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.10.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Mirko Da Corte Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

Currently if a DiscriminatorConvention is registered for object type, it is ignored.
Sample bug exposing code:

BsonSerializer.RegisterDiscriminatorConvention(typeof(object), new MyDiscriminatorConvention("_t"));

In BsonSerializer.LookupDiscriminatorConvention(Type type) we can read:

// inherit the discriminator convention from the closest parent (that isn't object) that has one
// otherwise default to the standard hierarchical convention
Type parentType = type.GetTypeInfo().BaseType;
while (convention == null)
{
    if (parentType == typeof(object))
    {
        convention = StandardDiscriminatorConvention.Hierarchical;
        break;
    }
    if (__discriminatorConventions.TryGetValue(parentType, out convention))
    {
        break;
    }
    parentType = parentType.GetTypeInfo().BaseType;
}

This is evidently a bug, also because before on the code we can read a passage where a default DiscriminatorConvention is assigned for object type, so it is not intended to be ignored:

if (type == typeof(object))
{
    // if there is no convention registered for object register the default one
    convention = new ObjectDiscriminatorConvention("_t");
    RegisterDiscriminatorConvention(typeof(object), convention);
}



 Comments   
Comment by Mirko Da Corte [ 24/Dec/21 ]

I've created a PR.

Comment by Esha Bhargava [ 28/Sep/20 ]

mirko@etherna.io Thanks for reporting this! At first glance, this looks like a bug. We are putting it in our backlog for now and will work on this when resources allow.

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