LiteDB: [BUG] NullReferenceException on Insert with auto id and BsonDocument with `_id=0`

NullReferenceException on Insert with auto id and BsonDocument with _id=0

Version

LiteDB 5.0.2 and the latest from repo, Windows 10, net45

Describe the bug

  1. Given a LiteCollection<BsonDocument> with auto id Int32.
  2. Insert a BsonDocument with _id = 0.
  3. -> NullReferenceException

Code to Reproduce

using (var db = new LiteDatabase(":memory:"))
{
    var test = db.GetCollection("Test", BsonAutoId.Int32);
    var doc = new BsonDocument() { ["_id"] = 0, ["p1"] = 1 };
    test.Insert(doc); // -> NullReferenceException
}

Expected behavior

This scenario works without exceptions and maybe sets the _id in the input document to the generated value.

But if for some reason this scenario is not valid then the exception should be different, some clear error.

Stack

>   LiteDB.dll!LiteDB.LiteCollection<LiteDB.BsonDocument>.Insert(LiteDB.BsonDocument entity) Line 27    C#
    MyLiteDB.exe!TryLiteDB.Program.Main(string[] args) Line 17  C#

Failed code due to null _id:

// checks if must update _id value in entity
if (removed)
{
    _id.Setter(entity, id.RawValue);
}

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

I will back into this issue before new release

Thanks again @nightroman, this 3th-party fine tests are great for LiteDB