Object Serializer breaking changes in 2.19 (.NET Client)

Hi all,

A new version of the mongodb driver for .NET was released under the minor version 2.19. This breaking change affects our MongoDB persistence and our customers. We have deliberately left the nuget range a bit more open because we hoped for SemVer compliance :wink: Anyway that’s not the topic I would like to discuss.

I was wondering why it is required to register a serializer mapping when there is already a BsonClassMap concept? Currently the breaking change guidance states the following:

var objectSerializer = new ObjectSerializer(type => ObjectSerializer.DefaultAllowedTypes(type) || type.FullName.StartsWith("MyNamespace"));
BsonSerializer.RegisterSerializer(objectSerializer);

but for us since parts of the serialization is done “by our framework” we cannot override the default serializer like that. So we need to register basically an object serializer per type we are aware of to not interfere with the user defined types because they will also have to configure things for all the types they allow. That being said the driver also has support for BsonClassMap which we are also using. For example we do the following for the types we discovered

foreach (var sagaMetadata in sagaMetadataCollection)
{
	if (!BsonClassMap.IsClassMapRegistered(sagaMetadata.SagaEntityType))
	{
	  var classMap = new BsonClassMap(sagaMetadata.SagaEntityType);
	  classMap.AutoMap();
	  classMap.SetIgnoreExtraElements(true);

	  BsonClassMap.RegisterClassMap(classMap);
}

which to me means we are already by definition making the driver “aware of the types we allow and want to map”. So why is the extra step necessary for users that already define mappings?

Regards,
Daniel

Hi, @Daniel_Marbach,

You raise a valid point. I see that both you and Gareth Budden made the same feature request and you are aware of CSHARP-4581. My initial impression is that this is a reasonable approach. We will discuss it during our weekly triage meeting. Please follow that ticket for updates.

Sincerely,
James