[CSHARP-2096] Make EnumRepresentationConvention also affect collections of Enums Created: 15/Nov/17  Updated: 08/Feb/23

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

Type: Improvement Priority: Major - P3
Reporter: Daniel Hegener Assignee: Unassigned
Resolution: Unresolved Votes: 6
Labels: rp-track
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by CSHARP-2387 EnumRepresentationConvention BsonType... Closed
Epic Link: Improve Serialization
Backwards Compatibility: Fully Compatible

 Description   

As someone mentioned on SO (https://stackoverflow.com/questions/47313022/mongodb-c-sharp-driver-serializing-listenum-as-string), the following cases are currently not handled by the EnumRepresentationConvention:

List<EnumType> { get; set; }
List<List<List<EnumType>>> { get; set; }
Dictionary<EnumType, OtherType> { get; set; }
Dictionary<OtherType, EnumType> { get; set; }

The convention currently only works on straight Enums as in

EnumType E { get; set; }

and nullable Enums as in:

EnumType? E { get; set; }

This improvement is about making sure that (even nested) collections of the various kinds are affected by the convention as expected.



 Comments   
Comment by James Kovacs [ 21/Nov/20 ]

We acknowledge that this ticket has been sitting idle for a considerable amount of time and appreciate everyone's continued interest in these improvements to the MongoDB .NET Driver. We are investing in improvements to make serialization behaviour in the driver more intuitive and less error prone. This is an example where the driver does not behave as expected necessarily.

If you enable the EnumRepresentationConvention or annotate an enum property/field with [BsonRepresentation(BsonType.String)], the enum is serialized as its string representation rather than its integer representation as expected. A community-submitted PR was intended to address the case where you are serializing a collection of enums and wish each one serialized as a string. Unfortunately the PR only works for List<T> and Dictionary<K,V>. It does not work for arrays of enums (T[]), other collection types (queues, stacks, linked lists, etc.), nor collection interfaces (IList<T>, IDictionary<K,V>, IEnumerable<T>, IQueryable<T>, ICollection<T>, etc.). Even if we supported all these collection types and interfaces, it would not work for custom collection types.

Rather than focusing on enums, let's consider the more general case of serialization conventions on collection types. The .NET Driver does not currently support the following either:

[BsonRepresentation(BsonType.String)]
public IEnumerable<ObjectId> ObjectIdCollection { get; set; }

What is the expected result? Should each item in the collection be serialized as a string or should the entire collection be serialized as a single comma-separated string?

Another case to consider is that of Dictionary<K,V>:

[BsonRepresentation(BsonType.String)]
public Dictionary<ObjectId, int> SomeDictionary { get; set; }

Should both keys and values be serialized as strings? Should we add a Target parameter to specify what it applies to? Do we allow applying two BsonRepresentationAttributes, one for Key and another for Value?

The original problem of serializing collections of enums as their string representation can be solved either with a custom serializer on the containing type or for the enum itself. The other two mentioned cases can be solved similarly.

We realize that this is not an ideal solution and we would like to provide better serialization semantics in the driver. We are reviewing serialization feature requests and bugs in an attempt to implement a more consistent and holistic approach to address problems such as this. We appreciate your patience and continued feedback.

Comment by Rasmus Sigsgaard [ 05/Jun/20 ]

I am evaluating Mongodb in a C# environment, and also encountered this issue. It seems to also affect arrays of enums (and possibly all collections).

Please consider merging https://github.com/mongodb/mongo-csharp-driver/pull/305

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