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

Serialization fails when implementation of IDictionary member does not have a public parameterless constructor

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.3
    • Affects Version/s: 2.0, 2.0.1
    • Component/s: BSON, Serialization

      When attempting to serialize a property of IDictionary<T,U>, if the implementing class does not contain a public parameterless constructor then serialization will fail with exception:

      System.ArgumentException: GenericArguments[0], 'Microsoft.FSharp.Core.ExtraTopLevelOperators+CreateDictionary@45[System.String,System.DateTime]', on 'MongoDB.Bson.Serialization.Serializers.DictionaryInterfaceImplementerSerializer`3[TDictionary,TKey,TValue]' violates the constraint of type 'TDictionary'. ---> System.TypeLoadException: GenericArguments[0], 'Microsoft.FSharp.Core.ExtraTopLevelOperators+CreateDictionary@45[System.String,System.DateTime]', on 'MongoDB.Bson.Serialization.Serializers.DictionaryInterfaceImplementerSerializer`3[TDictionary,TKey,TValue]' violates the constraint of type parameter 'TDictionary'.
      

      Example above (stack trace attached) from setting property as return of dict operator in F# Core https://msdn.microsoft.com/en-us/library/ee353774.aspx the implementation of which is the result of an object expression and therefore does not contain a public constructor.

      Attached is a unit test that works correctly in 1.x but does not work in 2.x (non F# but same principle).

      Exception is resolvable by changing signature of class DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue> to remove the new() constraint, and then changing implementation of DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue>.CreateInstance() to return Activator.CreateInstance<TDictionary>()

        1. CSHARP-1447-Unified Diff Since Head.patch
          4 kB
        2. DictionaryTests.fs
          1 kB
        3. MongoDictionarySerializationTest.cs
          3 kB
        4. stacktrace.txt
          8 kB

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            jhadwen James Hadwen
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: