[CSHARP-4636] System.Decimal serialization/deserialization is contains breaking changes Created: 04/May/23  Updated: 10/May/23  Resolved: 10/May/23

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

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

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

The new System.Decimal serialization added by extension of the ObjectSerlializer generates the new JSON structure, which does not contain the type discriminator.
After the deserialization, the data type is unexpected.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

2.19.1

How to Reproduce

public class DecimalDeserialization
{
    [Theory]
    [InlineData("""{ "Value" : { "_t" : "System.Decimal", "_v" : NumberDecimal("1.5") } }""")] // Old format <= 2.19.0; Success
    [InlineData("""{ "Value" : NumberDecimal("1.5") }""")] // New format 2.19.1. Error: Expected type to be System.Decimal, but found MongoDB.Bson.Decimal128.
    public void ObjectSerializer_should_deserialize_decimals_successfully(string jsonValue)
    {
        var rehydrated = BsonSerializer.Deserialize<Data>(jsonValue);
        ((decimal)rehydrated.Value).Should().Be(1.5m);
        rehydrated.Value.Should().BeOfType<decimal>();
    }
 
    private sealed record Data(object Value);
} 

Additional Background

The expected deserialization result of the instance of System.Decimal type should be the other instance System.Decimal type with the same value.



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

Please see CSHARP-4648

Comment by James Kovacs [ 09/May/23 ]

Hi, yurii.zhoholiev@gmail.com,

Thank you for raising this issue. It has the same underlying root cause as CSHARP-4637. 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 Yurii Zhoholiev [ 04/May/23 ]

This affects any boxed decimal serialization/deserialization process.
It seems, that this issue was introduced by the CSHARP-4496: Add decimal support to ObjectSerializer. (#1024) · mongodb/mongo-csharp-driver@0eb71aa (github.com)

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

Hi yurii.zhoholiev@gmail.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.