[CSHARP-2730] Exception when applying BsonRepresentation to ReadOnlyCollection Created: 05/Sep/19  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.9.1
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Taylor Graham Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

If I try do the following:

[BsonRepresentation(BsonType.ObjectId)]
public ReadOnlyCollection<string> References { get; private set; }

I get an exception when registering the class:

 

System.NotSupportedException: A serializer of type 'ReadOnlyCollectionSerializer<String>' is not configurable using an attribute of type 'BsonRepresentationAttribute'

 

I suspect this is because the ReadOnlyCollectionSerializer does not implement the IChildSerializerConfigurable interface, unlike most other collection serializers. Is there a reason for this?

 



 Comments   
Comment by Taylor Graham [ 05/Sep/19 ]

FYI as a workaround, I can do this when registering the class:

cm.MapProperty(x => x.References)
    .SetSerializer(new ReadOnlyCollectionSerializer<string>(new StringSerializer(BsonType.ObjectId)))

However I'd prefer not having to do this with each member since it makes the code more difficult to reason with.

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