[CSHARP-4205] Serialization error for generic enumerable not implementing System.Collections.Generic.List Created: 13/Jun/22  Updated: 28/Oct/23  Resolved: 23/Jun/22

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: None
Fix Version/s: 2.17.0

Type: Bug Priority: Unknown
Reporter: Ming Yau Lee Assignee: James Kovacs
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: Improve Serialization
Backwards Compatibility: Minor Change

 Description   

Summary

When a custom collection property is serialized, an exception is thrown because MongoDB.Bson default serialization provider implicitly assumes any IEnumerable<T> can be serialized as type List<T>.

Version: v2.16.0

How to Reproduce

using MongoDB.Bson;
using System.Collections;
using System.Collections.Generic;
 
namespace MongoDbBsonTest
{
    public static class Program
    {
        public static void Main()
        {
            var obj = new Node()
            {
                Children = new FooCollection()
                {
                    new Node() { Prop = "Test" }
                }
            };
            var bson = obj.ToBson<object>();
        }
    }
 
    public class Node
    {
        public string Prop { get; set; }
        public INodeCollection Children { get; set; }
    }
 
    public interface INodeCollection : ICollection<Node>
    {
    }
 
    public class FooCollection : INodeCollection
    {
        private readonly List<Node> data = new();
        public int Count => data.Count;
        public bool IsReadOnly => false;
        public void Add(Node item) => data.Add(item);
        public void Clear() => data.Clear();
        public bool Contains(Node item) => data.Contains(item);
        public void CopyTo(Node[] array, int arrayIndex) => data.CopyTo(array, arrayIndex);
        public IEnumerator<Node> GetEnumerator() => data.GetEnumerator();
        public bool Remove(Node item) => data.Remove(item);
        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    }
} 



 Comments   
Comment by Githook User [ 23/Jun/22 ]

Author:

{'name': 'James Kovacs', 'email': 'jkovacs@post.harvard.edu', 'username': 'JamesKovacs'}

Message: Added Ming Yau Lee as a contributor for implementing CSHARP-4205.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/c4ee32b50220d2c4d9a896f86394c53c740ca0cb

Comment by Githook User [ 23/Jun/22 ]

Author:

{'name': 'mingyaulee', 'email': 'lee_mingyau@hotmail.com', 'username': 'mingyaulee'}

Message: CSHARP-4205: Fix serialization of generic enumerable not implementing List<T> (#819)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/3ef4505e2fe28956500cea431d69063358c08a37

Comment by James Kovacs [ 16/Jun/22 ]

Hi, lee_mingyau@hotmail.com,

Thank you for reporting this issue and providing a pull request. Please provide unit tests to demonstrate that the change fixes the issue reported and does not break existing functionality. The repro shared above is a good starting point for these unit tests. Thank you in advance for your contribution to the MongoDB .NET/C# Driver.

Sincerely,
James

Generated at Wed Feb 07 21:47:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.