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

[BsonSerializer] property attribute with custom dictionary breaks in 2.19

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.19.0
    • Component/s: None
    • None
    • 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?
    • None
    • None
    • None
    • None
    • None
    • None

      In version 2.18 (and before) we used a property attribute to define a custom dictionary serializer as below:

      [BsonSerializer(typeof(CustomDictionarySerializer<Dictionary<AccountConfigType, string>,  EnumStringSerializer<AccountConfigType>, CustomObjectSerializer<string>>))]

      But after upgrading to 2.19,  we would get the error:

      System.TypeInitializationException : The type initializer for 'i3vDB.Collections.Account' threw an exception.
        ----> System.ArgumentException : Value type of serializer is System.Collections.Generic.Dictionary`2[[Lib.Shared.Enum.AccountConfigType, Lib.Shared, Version=2.1.4.1, Culture=neutral, PublicKeyToken=null],[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] and does not match member type System.Collections.Generic.Dictionary`2[[Lib.Shared.Enum.AccountConfigType, "Lib.Shared ", Version=2.1.4.1, Culture=neutral, PublicKeyToken=null],[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]. (Parameter 'serializer')

       

      Removing the property attribute, and instead using the below fixes the issue.

      BsonSerializer.RegisterGenericSerializerDefinition(typeof(Dictionary<AccountConfigType, string>),  typeof(CustomDictionarySerializer<Dictionary<AccountConfigType, string>,           EnumStringSerializer<AccountConfigType>, CustomObjectSerializer<string>>));

       

      I should also note that if you have both the property attribute and the explicit registration, it throws the same exception.    I did not test any other custom serializers as we didn't have any others specified by property attribute.

      I don't know if it's significant as it could just be caused by the exception output, but the TYPE X doesn't match TYPE X message only differs in Lib.Shared.   The second instance is in quotes and has a space at the end.   They are otherwise identical.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            tim.arheit@payschools.com Tim Arheit
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: