[CSHARP-4534] Consider adding anonymous types to DefaultAllowedTypes Created: 18/Feb/23  Updated: 21/Jul/23  Resolved: 21/Jul/23

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

Type: Improvement Priority: Unknown
Reporter: Boris Dogadov Assignee: Robert Stam
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-4532 We're no longer able to use dynamic a... Closed
is related to CSHARP-4475 Add an AllowedTypes filter to ObjectS... Closed
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Comments   
Comment by Githook User [ 21/Jul/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4534: Allow anonymous types in ObjectSerializer.DefaultAllowedTypes.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/e0c14c80e6c31f337439d2915b5dd90fe38f9562

Comment by Boris Dogadov [ 12/Jul/23 ]

Hi erik.hennerfors@laget.se
we are going to reevaluate this ticket, and provide updates here.

Thanks.

Comment by Erik Hennerfors [ 03/Jul/23 ]

Hi
Is there any updates for the work on this?

Kind regards
Erik

Comment by Erik Hennerfors [ 23/Feb/23 ]

Hi
I get the decision to add the AllowedTypes filter to the ObjectSerializer, but maybe the solution was hastily implemented.

One of the strengths, as I see it, of a document database is that you don't need to type everything. For example, we use MongoDB to store entity audit logs where we have dynamic fields for properties changed on the entity, this is now unusable if we upgrade to 2.19.0

At least a better example should be provided of how to use dynamic as a property type, since the one we've seen being documented does not work.

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

Adding this into for example the `Configure` method in `Startup.cs` will only result in the error BsonSerializationException: There is already a serializer registered for type Object.

UPDATE
We got the documented solution above wokring by adding it before IHostBuilder, so it is the first thing that are beeing executed in the Main method.

Kind regards
Erik

Comment by Robert Stam [ 21/Feb/23 ]

The important question to answer is whether this is safe. We don't want to defeat the security added by CSHARP-4475.

Most likely an anonymous type can be considered safe for serialization/deserialization if all of its properties are themselves safe for serialization/deserialization.

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