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

MongoDB C# Driver (2.7) and serialization issues

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.7.0
    • Component/s: Serialization
    • Labels:
      None
    • Environment:
      .net framework 4.5

      The following post is more or less a copy-paste of a question I posted on stackoverflow.

      My colleagues suggested me to also post a question here on JIRA, so here I am.

      I also provided a minimalistic sample code to reproduce the error.

      Link to SO: https://stackoverflow.com/questions/51285902/mongodb-c-sharp-driver-2-7-and-serialization-issues

      ===== ISSUE AND QUESTION ====

      I'm currently having a strange issue with the latest mongodb c# driver (2.7), inheritance and serialization.I'm currently having a strange issue with the latest mongodb c# driver (2.7), inheritance and serialization.

      Here is a super simplified class hierarchy that I'm using:

      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
      public abstract class BaseClass { 
        public Guid Id { get; private set; }
        protected BaseClass(Guid id) { Id = id; } 
      }     
      
      public class DerivedClass : BaseClass 
      { 
        public DerivedClass(Guid id) : base(id) {} 
      }
      

      When I try to register the class for mongo serialization, I am getting an error:

      var type = typeof(DerivedClass);
      if (BsonClassMap.IsClassMapRegistered(type)) 
        return;
      var cm = new BsonClassMap(type); cm.AutoMap();
      BsonClassMap.RegisterClassMap(cm);
      

      The error is:

      The memberInfo argument must be for class DerivedClass, but was for class BaseClass.    Parameter name: memberInfo
      

      Now, and this is where things start to get weird, when I tried to understand what was happening I made some changes to the BaseClass or the DerivedClass.

      ANY of the two changes listed below will make the `RegisterClassMap` method work without errors...

      change 1: adding a second, unused property on BaseClass

      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
      public abstract class BaseClass { 
        public Guid Id { get; private set; } 
        public string Test { get; }
        protected BaseClass(Guid id) { Id = id; } 
      }
      

      change 2: changing the Id property to have a non-private setter (any other works)

      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
      public abstract class BaseClass { 
        public Guid Id { get; protected set; }
        protected BaseClass(Guid id) { Id = id; } 
      } 
      

      THE QUESTION
      My question is... what is happening? While I may agree that for the serializer you should have an accessible setter for every property on the class that you want to map (in this case, DerivedClass), why the case #1 is actually not giving any error?

       

            Assignee:
            james.kovacs@mongodb.com James Kovacs
            Reporter:
            sh0uzama Paolo Lombardo
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: