mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

[BUG] Arrays don't work when using custom constructors #2385

Open athendrix opened 1 year ago

athendrix commented 1 year ago

Version 5.0.17/Fedora Linux/.NET 6.0

Describe the bug Arrays don't work when using custom constructors

Code to Reproduce

public class MultiCtorWithArray
        {
            public int Id { get; set; }
            public string[] StrArr { get; set; }
            public string Name { get; set; }
            public string DefinedOnlyInStrArray { get; set; }

            public MultiCtorWithArray()
            {
            }

            [BsonCtor]
            public MultiCtorWithArray(int id, string[] strarr)
            {
                this.Id = id;
                this.StrArr = strarr;
                this.DefinedOnlyInStrArray = "changed";
            }

            public MultiCtorWithArray(int id, string[] strarr, string name)
            {
                this.Id     = id;
                this.StrArr = strarr;
                this.Name   = name;
            }
        }
        [Fact]
        public void BsonCtorWithArray_Attribute()
        {
            var doc = new BsonDocument { ["_id"] = 25, ["name"] = "value-name", ["strarr"] = new BsonArray() {"foo","bar"} };

            var obj = _mapper.ToObject<MultiCtorWithArray>(doc);

            obj.Id.Should().Be(25);
            obj.Name.Should().Be("value-name");
            string.Join(", ", obj.StrArr).Should().Be("foo, bar");
            obj.DefinedOnlyInStrArray.Should().Be("changed");
        }

Expected behavior This test (based off the BsonCtor_Attribute test) should pass, but it fails.

athendrix commented 1 year ago

Fixed by pull request #2384