[CSHARP-4637] Error in deserializing Decimal stored as object in driver 2.19 Created: 05/May/23  Updated: 10/May/23  Resolved: 10/May/23

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

Type: Bug Priority: Unknown
Reporter: Gian Maria Ricci Assignee: Robert Stam
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2023-05-05-11-22-32-577.png    
Issue Links:
Duplicate
is duplicated by CSHARP-4648 Standardize handling of decimal vs De... Closed
Related
is related to CSHARP-4648 Standardize handling of decimal vs De... Closed
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   

Summary

After upgrading to driver 2.19 we noticed integration test starts failing. The reason is in how the driver changed serialization of Decimal when saved as object.

We have a .NET type that has a type Value that contains an Object, then we can store different type and Bson Serialization does the magic. With old version of the driver here is how data is stored in MongoDb

"Value" : {
     "_t" : "System.Decimal",
     "_v" : "12"
},

When the data is deserialized we have a System.Decimal with value 12 in the property. Upgrading to 2.19 version of the driver we have this in the database

 "Value" : NumberDecimal("12"),

And the problem is that after deserialization we have value property with an object of type MongoDb.Bson.Decimal128. This breaks the software because it cannot be casted to decimal.

As you can see the check "is Decimal" fails also it cannot be casted to Decimal, but we need to use Convert.ToDecimal(). This is actually breaking our software that is based on pure .NET type checking.

From my point of view Serialization is broken because I save object with a decimal and I got back object of Bson.Decimal128.

Version of driver 2.19

How to Reproduce

MongoTest sut = new MongoTest() { Value = 12M };
Console.WriteLine(sut.Value is Decimal);
var serialized = sut.ToBson();
var deserialized = BsonSerializer.Deserialize<MongoTest>(serialized);
Console.WriteLine(deserialized.Value is Decimal);

public class MongoTest 

{     public Object Value \{ get; set; }

}

As you can see from the above code, I serialize an object with a decimal in the Value property, when I deserialize I have an internal class that is not Decimal. This actually breaks serialization.

 



 Comments   
Comment by Robert Stam [ 09/May/23 ]

Please see CSHARP-4648

Comment by James Kovacs [ 09/May/23 ]

Hi, alkampfer@nablasoft.com,

Thank you for raising this issue. It has the same underlying root cause as CSHARP-4636. We didn't intend to introduce breaking changes with the decimal serialization fix CSHARP-4496. We are currently working on a fix, which we plan to release in 2.19.2.

Sincerely,
James

Comment by Gian Maria Ricci [ 05/May/23 ]

 It seems related to CSHARP-4636

Introduced with https://jira.mongodb.org/browse/CSHARP-4496

Comment by Service Account: DBX TPM [ 05/May/23 ]

Hi alkampfer@nablasoft.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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