Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2387

EnumRepresentationConvention BsonType.String doesn't serialize enums to strings if the enums are in a list

    • Type: Icon: New Feature New Feature
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.7.0
    • Component/s: Serialization
    • Labels:
      None
    • Environment:
      Windows 10, 64-bit architecture, .NetFramework 471

      Create a serialization convention like so:

       

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

       

      Then define an enum like so:

       

      [PublicAPI]
      public enum JobCategory
      {
         Remodel,
         Repair
      }
      

       

      Finally add a list of enums to your data model

       

      public class JobModel
      {
          public List<JobCategory> JobCategories { get; set; } = new List<JobCategory>();
      }
      

       

      Write to mongodb then inspect the bson doc and notice that `JobCategory` is a list of int32 and not a list of strings. 

       

       

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            jdstapleton Jeffrey Stapleton
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: