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

Cannot deserialize inherited members with aggregation queries (fluent API)

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.0.1, 2.1
    • Affects Version/s: 2.0
    • Component/s: Serialization
    • None
    • Environment:
      Windows 8.1 x64, locally hosted mongodb, VS 2013, MSTest

      Cannot deserialize inherited members with aggregation queries (fluent API)

      Given class hierarchy:

          public class Aggregate : Entity
          {
              public string Id { get; set; }
          }
      
          public class Entity
          {
              public IEnumerable<LocalizableText> Name { get; set; }
          }
      
          public class Branch : Aggregate
          {
              public IEnumerable<LocalizableText> Description { get; set; }
              public string PartnerId { get; set; }
              public double Latitude { get; set; }
              public double Longitude { get; set; }
              public string Timetable { get; set; }
              public IEnumerable<Discount> Discounts { get; set; }
              public IEnumerable<Category> Categories { get; set; }
              public IEnumerable<Phone> Phones { get; set; }
              public byte[] Icon { get; set; }
              public byte[] Image { get; set; }
          }
      
      

      Given mappings:

                  BsonClassMap.RegisterClassMap<Entity>(cm =>
                  {
                      cm.SetIsRootClass(true);
                      cm.MapProperty(e => e.Name);
                      cm.SetDiscriminator(typeof(Entity).Name);
                      cm.AddKnownType(typeof(Aggregate));
                      cm.AddKnownType(typeof(Branch));
                      
                  });
                  BsonClassMap.RegisterClassMap<Aggregate>(cm =>
                  {
                      cm.SetIsRootClass(true);
                      cm.SetDiscriminator(typeof(Aggregate).Name);
                      cm.SetIdMember(cm.GetMemberMap(a => a.Id));
                      cm.AddKnownType(typeof(Branch));
                  });
                 BsonClassMap.RegisterClassMap<Branch>(cm =>
                  {
                      cm.AutoMap();
                      cm.SetDiscriminator(typeof(Branch).Name);
                  });
      
      

      When running the query:

              public async Task<Branch> GetBranchToolTipAsync(string partnerId, string branchId)
              {
                  return await Collection.Aggregate()
                      .Match(x => x.PartnerId == partnerId)
                      .Group(x => x.PartnerId, g => new Branch()
                      {
                          PartnerId = g.Key,
                          Name = g.First(x => x.PartnerId == partnerId).Name,
                          Description = g.First(x => x.Id == branchId).Name,
                          Discounts = g.First(x => x.Id == branchId).Discounts,
                          Id = branchId
                      }).FirstOrDefaultAsync();
              }
      

      Then the following exception occurs:

      Test method Server.Application.Tests.Services.Partners.PartnerApplicationServiceTests.ShouldGetBranchToolTipAsync threw exception: 
      
      System.ArgumentOutOfRangeException: The memberInfo argument must be for class Branch, but was for class Entity.
      
      Parameter name: memberInfo
          at MongoDB.Bson.Serialization.BsonClassMap.EnsureMemberInfoIsForThisClass(MemberInfo memberInfo)
         at MongoDB.Bson.Serialization.BsonClassMap.MapMember(MemberInfo memberInfo)
         at MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.BuildProjectedSerializer(ProjectionMapping mapping)
         at MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.BuildMemberInit(MemberInitExpression node)
         at MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.Build(Expression node)
         at MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.Build(Expression node, IBsonSerializerRegistry serializerRegistry)
         at MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.TranslateGroup(Expression`1 idProjector, Expression`1 groupProjector, IBsonSerializer`1 parameterSerializer, IBsonSerializerRegistry serializerRegistry)
         at MongoDB.Driver.IAggregateFluentExtensions.GroupExpressionProjection`3.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
         at MongoDB.Driver.AggregateFluent`2.<>c__DisplayClass1`1.<Group>b__0(IBsonSerializer`1 s, IBsonSerializerRegistry sr)
         at MongoDB.Driver.DelegatedPipelineStageDefinition`2.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry)
         at MongoDB.Driver.PipelineStageDefinition`2.MongoDB.Driver.IPipelineStageDefinition.Render(IBsonSerializer inputSerializer, IBsonSerializerRegistry serializerRegistry)
         at MongoDB.Driver.PipelineStagePipelineDefinition`2.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry)
         at MongoDB.Driver.MongoCollectionImpl`1.<AggregateAsync>d__7`1.MoveNext()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
         at MongoDB.Driver.IAggregateFluentExtensions.<FirstOrDefaultAsync>d__b`1.MoveNext()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
         at SCNDISC.Server.Infrastructure.Persistence.Queries.Partners.BranchToolTipQuery.<GetBranchToolTipAsync>d__2.MoveNext() in BranchToolTipQuery.cs: line 16
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
         at SCNDISC.Server.Application.Services.Partners.PartnerApplicationService.<GetBranchToolTipAsync>d__9.MoveNext() in PartnerApplicationService.cs: line 46
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
         at SCNDISC.Server.Application.Tests.Services.Partners.PartnerApplicationServiceTests.<ShouldGetBranchToolTipAsync>d__35.MoveNext() in PartnerApplicationServiceTests.cs: line 87
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
      

            Assignee:
            craig.wilson@mongodb.com Craig Wilson
            Reporter:
            Cortlendt Dmitry Kuznetsov [X]
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: