Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3154

Can't register DiscriminatorConvention to object type

    XMLWordPrintableJSON

Details

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

      Attachments

        Activity

          People

            Unassigned Unassigned
            mirko@etherna.io Mirko Da Corte
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: