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

Projecting an enumerable as a top-level enumerable doesn't deserialize properly

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 2.1
    • Linq
    • None

    Description

      public static async Task TestAsync()
      {
          var client = new MongoClient();
          var db = client.GetDatabase("test");
          var col = db.GetCollection<User>("users");
       
          await db.DropCollectionAsync("users");
       
          await col.InsertOneAsync(new User
          {
              Id = "1",
              AppIntegrations = new[] 
              { 
                  new UserAppIntegration { AppName = "App1", CanRemove = true },
                  new UserAppIntegration { AppName = "App2", CanRemove = false },
              }
          });
       
          var pipeline = col.Aggregate()
              .Match(u => u.Id == "1")
              .Project(u => u.AppIntegrations.Select(i => new UserAppIntegrationStatus { AppName = i.AppName, CanRemove = !i.CanRemove }));
       
          var results = await pipeline.ToListAsync();
       
          //warm up
       
          Console.ReadKey();
      }
       
      public class User
      {
          public User()
          {
              AppIntegrations = new UserAppIntegration[0];
          }
       
          public string Id { get; set; }
          public string Email { get; set; }
          public UserAppIntegration[] AppIntegrations { get; set; }
      }
       
      public class UserAppIntegration
      {
          public string AppName { get; set; }
          public bool CanRemove { get; set; }
      }
       
      public class UserAppIntegrationStatus
      {
          public string AppName { get; set; }
          public bool CanRemove { get; set; }
      }
      

      Workaround

      Instead of projecting the enumerable directly, wrap it in a type:

      var pipeline = col.Aggregate()
                      .Match(u => u.Id == "1")
                      .Project(u => new { SomeField = u.AppIntegrations.Select(i => new UserAppIntegrationStatus { AppName = i.AppName, CanRemove = !i.CanRemove }) });
      

      Attachments

        Activity

          People

            craig.wilson@mongodb.com Craig Wilson
            craig.wilson@mongodb.com Craig Wilson
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: