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

[BsonDefaultValue(null, SerializeDefaultValue = false)] doesn't work as expected

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.0
    • Affects Version/s: 0.9
    • Component/s: None
    • None
    • Environment:
      Win 7

      If I have an object with a null on it for a property that is marked "[BsonDefaultValue(null, SerializeDefaultValue = false)]", I'd expect that no value would get written to the database. But what happens is that the test code to decide whether null == null blows up because of the way it is written.

      Looking at the code I can see that I should have used [BsonIgnoreIfNull] but why isn't [BsonIgnoreIfNull] equivalent to [BsonDefaultValue(null, SerializeDefaultValue = false)]?

      Suggest that with a small tweak this code could be made to work whichever way a user chooses to specify that null values are the default and are to be ignored.

      private void SerializeMember(
      BsonWriter bsonWriter,
      object obj,
      BsonMemberMap memberMap
      ) {
      var value = memberMap.Getter(obj);
      if (value == null && memberMap.IgnoreIfNull)

      { /// SUGGEST: Make this work if memberMap.DefaultValue == null too ... return; // don't serialize null value }

      if (memberMap.HasDefaultValue && !memberMap.SerializeDefaultValue && value.Equals(memberMap.DefaultValue))

      { return; // don't serialize default value }

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            ianmercer Ian Mercer
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: