[CSHARP-2127] Enums with e.g. uint underlyings fail to serialized Created: 13/Dec/17  Updated: 28/Oct/23  Resolved: 06/Jan/21

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.4.4
Fix Version/s: 2.12.0

Type: Improvement Priority: Major - P3
Reporter: Daniel Hegener Assignee: Boris Dogadov
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

As per this SO issue:

https://stackoverflow.com/questions/47781059/c-sharp-mongodb-tries-to-convert-enumuint32-to-int32-and-crashes

Easy to reproduce in EnumSerializerUInt32Tests.cs by switching from

        private enum E : uint
        {
            A = 1,
            B = 2
        }

to

        private enum E : uint
        {
            A = uint.MaxValue,
            B = 2
        }

and running the contained tests.

Result: Exception: "Value was either too large or too small for an Int32."

The problem here is in the EnumSerializer which attempts a Convert.ToInt32 for a Uint32 and a Convert.ToInt64 for a Uint64.



 Comments   
Comment by Boris Dogadov [ 06/Jan/21 ]

Hi dnickless
Thanks for bringing up this issue and submitting a fix!

We have fixed this taking a slightly different approach here.

Comment by Boris Dogadov [ 07/Dec/20 ]

rstam do we want to add support for ulong in the similar way?

Comment by Ian Whalen (Inactive) [ 11/Mar/19 ]

Doing some backlog grooming and evaluating these PRs on a one-by-one basis. Confirming here that we want to consider this for merge as soon as 4.2 work slows down.

Comment by Daniel Hegener [ 14/Dec/17 ]

Pretty old but related: https://jira.mongodb.org/browse/CSHARP-252 (covers the non Enum case)

Comment by Daniel Hegener [ 14/Dec/17 ]

PR: https://github.com/mongodb/mongo-csharp-driver/pull/310

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