[CSHARP-2041] EnumRepresentationConvention does not apply to arrays Created: 21/Sep/17  Updated: 27/Oct/23  Resolved: 25/Sep/17

Status: Closed
Project: C# Driver
Component/s: Configuration, Serialization
Affects Version/s: 2.2.4, 2.3, 2.4.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Daniel Nauck Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-1728 EnumRepresentationConvention doesn't ... Closed

 Description   

Hello,

my goal is to store all enum types as string in MongoDB instead of the default int storage.

I'm using the following code:

ConventionRegistry.Register("StringEnumConvention",
                        new ConventionPack {new EnumRepresentationConvention(BsonType.String)}, t => true);

This code is executed before the first usage of MongoClient

But looks like this setting is completely ignored. Any ideas?



 Comments   
Comment by Robert Stam [ 25/Sep/17 ]

Note that conventions like EnumRepresentationConvention are only used when building a BsonClasMap, and apply to members of type E, not to all values of type E.

As written, this convention does not apply to members of type E[] (like Es).

To serialize arrays of E as strings you would need a separate convention that applies to members of type E[].

See CSHARP-1728 for a more complete explanation and for some techniques you could use if you want all values of type E to be serialized as strings.

Comment by Daniel Nauck [ 25/Sep/17 ]

Here is the code to reproduce:

 
 
 
 
namespace TestCSharp2041
 
{
 
    public enum E { A, B };
 
 
 
    public class C
 
    {
 
        public int Id { get; set; }
 
        public E E { get; set; }
 
        public E[] Es { get; set; }
 
    }
 
 
 
    public static class Program
 
    {
 
        public static void Main(string[] args)
 
        {
 
            ConventionRegistry.Register(
 
                "StringEnumConvention",
 
                new ConventionPack { new EnumRepresentationConvention(BsonType.String) },
 
                t => true);
 
 
 
            var client = new MongoClient("mongodb://localhost");
 
            var database = client.GetDatabase("test");
 
            var collection = database.GetCollection<C>("test");
 
            database.DropCollection("test");
 
 
 
            var c = new C { Id = 1, E = E.A, Es = new[] { E.A, E.B } };
 
            collection.InsertOne(c);
 
        }
 
    }
 
}

Comment by Robert Stam [ 21/Sep/17 ]

I am unable to reproduce this. I used the following program:

namespace TestCSharp2041
{
    public enum E {  A , B};
 
    public class C
    {
        public int Id { get; set; }
        public E E { get; set; }
    }
 
    public static class Program
    {
        public static void Main(string[] args)
        {
            ConventionRegistry.Register(
                "StringEnumConvention",
                new ConventionPack { new EnumRepresentationConvention(BsonType.String) },
                t => true);
 
            var client = new MongoClient("mongodb://localhost");
            var database = client.GetDatabase("test");
            var collection = database.GetCollection<C>("test");
            database.DropCollection("test");
 
            var c = new C { Id = 1, E = E.A };
            collection.InsertOne(c);
        }
    }
}

And verified that the proper enum representation was used by inspecting the collection using the MongoDB shell:

> db.test.find()
{ "_id" : 1, "E" : "A" }
>

Do you have a different scenario that can be used to reproduce what you are seeing?

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