[CSHARP-4565] [BsonSerializer] property attribute with custom dictionary breaks in 2.19 Created: 09/Mar/23  Updated: 27/Oct/23  Resolved: 23/Mar/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.19.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Tim Arheit Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

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?


 Description   

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.



 Comments   
Comment by Robert Stam [ 23/Mar/23 ]

Thank you for letting us know that you figured out the issue. I will go ahead and close this issue.

Comment by Tim Arheit [ 13/Mar/23 ]

Tracked the issue down to a trailing space in the assembly name.  Not sure why it didn't show up before. 

Generated at Wed Feb 07 21:48:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.