[CSHARP-2972] IgnoreIfDefault not working? Created: 19/Feb/20  Updated: 30/Mar/20  Resolved: 30/Mar/20

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.10.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Brecht Vanhaesebrouck Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10



 Description   

Setting IgnoreIfDefault to true through the BsonClassMap.RegisterClassMap call does not seem to work.

Eg:

doc.MapMember(x => x.TriesDone).SetIgnoreIfDefault(true);

TriesDone gets serialized to the database with a default value of 0. I am having the same issue with reference types set to null.



 Comments   
Comment by Robert Stam [ 10/Mar/20 ]

I am unable to reproduce this.

I couldn't use all of your code because it references types that weren't provided. But since your initial description mentioned the TriesDone property I simplified the example to:

namespace TestCSharp2972
{
    public class Event
    {
        public String Id { get; set; }
        public Int32 TriesDone { get; set; }
    }
 
    public static class Program
    {
        public static void Main(string[] args)
        {
            BsonClassMap.RegisterClassMap<Event>(doc =>
            {
                doc.AutoMap();
                doc.SetIgnoreExtraElements(true);
 
                doc.MapIdMember(a => a.Id)
                    .SetIdGenerator(new StringObjectIdGenerator())
                    .SetSerializer(new StringSerializer(BsonType.ObjectId));
 
                doc.MapMember(x => x.TriesDone)
                    .SetIgnoreIfDefault(true);
            });
 
            var e = new Event
            {
                Id = "112233445566778899aabbcc",
                TriesDone = 0
            };
            var json = e.ToJson();
            // json == "{ \"_id\" : ObjectId(\"112233445566778899aabbcc\") }"
        }
    }
}

Note that serialized form (in JSON here) does NOT contain the TriesDone field.

 

Comment by Brecht Vanhaesebrouck [ 20/Feb/20 ]

Here's the full class map:

 
BsonClassMap.RegisterClassMap<Event>(doc =>
{
    doc.AutoMap(); 
    doc.SetIgnoreExtraElements(true); 
 
    doc.MapIdMember(a => a.Id) 
        .SetIdGenerator(new StringObjectIdGenerator()) 
        .SetSerializer(new StringSerializer(BsonType.ObjectId)); 
 
    doc.MapMember(x => x.State) 
        .SetSerializer(new EnumSerializer<EventState>(BsonType.String)); 
 
    doc.MapMember(x => x.NextTryAt)
        .SetIgnoreIfDefault(true);
 
    doc.MapMember(x => x.TriesDone)
        .SetIgnoreIfDefault(true); 
 
    doc.MapMember(x => x.FailedTries)
        .SetIgnoreIfDefault(true)
        .SetDefaultValue(new List<FailedTry>());
});

This is the model itself:

public class Event : IEntity
{
    public String Id { get; set; }
    public String EventId  { get; set; }
    public String ConcurrencyStamp  { get; set; }
 
    public String Source  { get; set; }
    public String Type  { get; set; }
    public String Subject { get; set; }
    public DateTime? PublishedAt { get; set; }
 
    public IEvent StructuredEvent { get; set; }
    public EventState State { get; set; }
    public DateTime AddedAt { get; set; }
    public DateTime? ProcessedAt { get; set; }
 
    public Int32 TriesDone { get; set; }
    public DateTime? NextTryAt { get; set; }
 
    public IList<FailedTry> FailedTries { get; set; } = new List<FailedTry>();
}

Comment by Dmitry Lukyanov (Inactive) [ 19/Feb/20 ]

Hello brecht.vanhaesebrouck@sea-invest.com,

can you please provide more full repro. In particular the full class description.

Generated at Wed Feb 07 21:44:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.