Details
-
Bug
-
Resolution: Unresolved
-
Major - P3
-
None
-
2.10.4
-
None
-
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); |
}
|