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

Can't register DiscriminatorConvention to object type

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.10.4
    • Component/s: Serialization
    • Labels:
      None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

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

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      BsonSerializer.RegisterDiscriminatorConvention(typeof(object), new MyDiscriminatorConvention("_t"));
      

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

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      // 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:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      if (type == typeof(object))
      {
          // if there is no convention registered for object register the default one
          convention = new ObjectDiscriminatorConvention("_t");
          RegisterDiscriminatorConvention(typeof(object), convention);
      }
      

            Assignee:
            adelin.mbidaowona@mongodb.com Adelin Mbida Owona
            Reporter:
            mirko@etherna.io Mirko Da Corte
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: