[CSHARP-1385] Unable to explicitly set serializer for recursive enumerable types Created: 23/Aug/15  Updated: 02/Apr/16  Resolved: 28/Jan/16

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.0
Fix Version/s: 2.1

Type: Bug Priority: Critical - P2
Reporter: David Pfeffer Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CSHARP-1296 StackOverflowException when serializi... Closed

 Description   

The following Main function will blow up, due to the constructor for ArraySerializer registering a class map for the type before the class map can be registered.

void Main()
{
	BsonClassMap.RegisterClassMap<ModelType>(x => {
		x.MapMember(y => y.ModelTypes).SetSerializer(new ArraySerializer<ModelType>());
	});
}
 
// Define other methods and classes here
public class ModelType
{
	public ModelType[] ModelTypes { get; set; }
}



 Comments   
Comment by Robert Stam [ 28/Jan/16 ]

I have verified that the fix to CSHARP-1296 also fixes this issue. So am closing this as fixed in 2.1.

Comment by Robert Stam [ 24/Aug/15 ]

Agreed. It should be lazy, and the change made for CSHARP-1296 makes it lazy.

Comment by David Pfeffer [ 24/Aug/15 ]

IMO the default behavior should be lazy. This caused a pretty major breaking change sort of bug when I just tried to migrate to 2.0 from 1.x. I had to write my own serializer to avoid it. This current bug/design decision is nonobvious until you dig into the source.

Comment by Robert Stam [ 24/Aug/15 ]

This seems to be related to CSHARP-1296, which has been fixed for the upcoming 2.1 version of the driver.

There is a new constructor for ArraySerializer<T> that enables delaying the choosing of the serializer for <T> by passing in a serializerRegistry:

https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Bson/Serialization/Serializers/ArraySerializer.cs#L50

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