Error when model has dynamic property ASP .NET Core 6

Hello!
I am using ASP .NET Core 6 and MongoDB.Driver 2.21.0 I need a model that has a dynamic or generic property.

The property in question is declared as follows:

    public dynamic data { get; set; }

After creating, the services and the controller, when I try to create using swagger in mongoDB I get this error:

MongoDB.Bson.BsonSerializationException: An error occurred while serializing the data property of class prueba.Models.Servicio.Servicio: Type System.Text.Json.JsonElement is not configured as a type that is allowed to be serialized for this instance of ObjectSerializer.
 ---> MongoDB.Bson.BsonSerializationException: Type System.Text.Json.JsonElement is not configured as a type that is allowed to be serialized for this instance of ObjectSerializer.
   at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.SerializeDiscriminatedValue(BsonSerializationContext context, BsonSerializationArgs args, Object value, Type actualType)
   at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, Object value)
   at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Serialize(IBsonSerializer serializer, BsonSerializationContext context, Object value)
   at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.SerializeMember(BsonSerializationContext context, Object obj, BsonMemberMap memberMap)

I have been investigating and I have not found any solution, Could you tell me how to solve it?

Thanks in advance.

Hi, @Roque_Rojo_Bacete,

Welcome to the MongoDB Community Forums. I understand that you’re having a problem serializing a dynamic type. Due to a .NET vulnerability with type descriptors, we require you to explicitly opt into safe types when using the ObjectSerializer. You can find details in our FAQ:

What Object Types Can Be Serialized?

Sincerely,
James