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

[BsonIgnore] in deserialization

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 2.8.0
    • BSON
    • .NET Core 2.2 on Windows 8

    Description

      Consider the following implementation:

      public class Foo
      {
      	public ObjectId Id { get; set; }
      	public string Name { get; set; }
      }
       
      public class Bar
      {
      	public ObjectId Id { get; set; }
      	public ObjectId FooId { get; set; }
       
      	[BsonIgnore]
      	public Foo Foo { get; set; }
      }
      

      When saving a Bar instance into the Collection the Foo property is ignored as expected. But the property is also being ignored when deserializing Bar.

       

       

      return await db.GetCollection<Bar>("Bar")
      	.Aggregate()
      	.Lookup("Foo", "FooId", "_id", "Foo")
      	.Unwind("Foo")
      	.As<Bar>()
      	.ToListAsync(cancellationToken);
      

      The code above does not work since the driver is ignoring the property Foo of Bar's class.

       

      'Element 'Foo' does not match any field or property of class Bar.'

      Why [*BsonIgnored*] is being used in deserialization?

       

      Interestingly, if a change the Bar class as following (using ShouldSerialize method) the code works as expected:

      public class Bar
      {
      	public ObjectId Id { get; set; }
      	public ObjectId FooId { get; set; }
       
      	public Foo Foo { get; set; }
      	public bool ShouldSerializeFoo() { return false; }
      }
      

      Any thoughts on that?

      Attachments

        Activity

          People

            wan.bachtiar@mongodb.com Wan Bachtiar
            dlumeida@gmail.com Daniel Almeida
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: