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] Serializer does not use type parameter #2223

Open
marinusbeck opened this issue Aug 11, 2022 · 0 comments
Open

[BUG] Serializer does not use type parameter #2223

marinusbeck opened this issue Aug 11, 2022 · 0 comments
Labels

Comments

@marinusbeck
Copy link

Version
LiteDB version 5.0.12 / Windows 11 / .Net framework 4.7

Describe the bug
When serializing a derived class as the base class type it gets serialized as the derived type.

Code to Reproduce

        class BaseClass
        {
            public int A { get; set; }
        }

        class DerivedClass : BaseClass
        {
            public int B { get; set; }
        }

        [Fact]
        public void Serialize_covariant_type_uses_type_parameter()
        {
            var derived = new DerivedClass()
            {
                A = 1,
                B = 2,
            };

            var mapper = new BsonMapper();

            var docA = mapper.Serialize<BaseClass>(derived);
            var docB = mapper.Deserialize<DerivedClass>(docA);

            Assert.True(docB.B == 0); // expected == 0, actual == 2
        }

Expected behavior
The expected would be 0 in the above assert, but is actually 2. This is because the mapper reflects on the input object and not the type parameter when serializing.

marinusbeck pushed a commit to marinusbeck/LiteDB that referenced this issue Aug 11, 2022
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