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

Enum with ushort underlying type is not serialized correctly

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 3.3.0
    • Component/s: LINQ
    • None
    • None
    • Dotnet Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Using the following class:

      public class C
      {
          public int Id { get; set; }
          [BsonRepresentation(BsonType.String)] public E1 E1 { get; set; }
          [BsonRepresentation(BsonType.String)] public E2 E2 { get; set; }
      }                                                                   
      

      where E1 and E2 are enums:

      public enum E1
      {
          A = 1,
          B = 2
      }
      
      public enum E2 : ushort
      {                      
          A = 1,             
          B = 2              
      }                      
      

      The following query:

      var result = queryable.FirstOrDefault(x => x.E1 == E1.B);
      

      is correctly translated to:

      "{ $match : { E1 : 'B' } }"
      "{ $limit : 1 }");         
      

      but a similar query using E2 instead:

      var result = queryable.FirstOrDefault(x => x.E2 == E2.B);
      

      is incorrectly translated to:

      "{ $match : { E2 : 2 } }"
      "{ $limit : 1 }");         
      

      The issue is related to enum E2 being declared with an underlying type of ushort.

            Assignee:
            Unassigned Unassigned
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: