Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] new BsonValue(new string[] { "a", "b", "c" }).AsArray returns null #2456

Open
ericnewton76 opened this issue Mar 20, 2024 · 2 comments
Labels

Comments

@ericnewton76
Copy link

ericnewton76 commented Mar 20, 2024

Version
checked 5.0.12, checked 5.0.19, (worked in 3.1.1)

Describe the bug
Creating a new object via BsonValue ctor with an array for the value returns a BsonValue instance with Type=Array and RawValue=["a","b","c"] but usage of AsArray fail with null (returns this as BsonArray which returns null)

Code to Reproduce

var strings = new string[] { "a", "b", "c" };
var bsonValueOfArray = new BsonValue(strings);

Assert.NotNull(bsonValueOfArray.AsArray);

Expected behavior
Assert.NotNull(new BsonValue(new string[] { "a", "b", "c" }).AsArray); to succeed

The problem becomes apparent in the Serialization code, where it sees a BsonValue with Type=Array, calls AsArray and causes a NullReferenceException

Of Note, I uncovered this specifically when using MongoDB.Bson (2.20) as a source:

//psuedocode atm
var mongoArray = new MongoDB.Bson.BsonArray({ "a", "b", "c" });

var doc=new LiteDB.BsonDocument();
doc["array"] = new LiteDB.BsonValue(mongoArray);
liteCollection.Add(doc); //fails at ([line 661 ](https://github.com/mbdavid/LiteDB/blob/f23f14c53e934e14d8cfd0f10a1d78d21b3b3deb/LiteDB/Document/BsonValue.cs#L661)
@ericnewton76
Copy link
Author

Since 3.1.1 worked, I went to

public BsonArray AsArray
and see that AsArray is significantly different, in that it correctly returns a new LiteDB.BsonArray typed instance.

I tried to trace back to when AsArray changed so significantly but ran out of time.

Here's a test to show it failing:

namespace LiteDB.Tests.Issues;

public class Issue2456_Tests
{
    [Fact]
    public void BsonValue_ctorWithArray_AsArray_isvalid()
    {
        var strings = new string[] { "a", "b", "c" };
        var bsonValueOfArray = new BsonValue(strings);

        BsonArray instance = bsonValueOfArray.AsArray;
        Assert.NotNull(instance);
    }

}

@ericnewton76
Copy link
Author

Of note, trying to create a dotnetfiddle for showing it too: https://dotnetfiddle.net/dA5BLM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant