-
Type: Bug
-
Resolution: Done
-
Priority: Minor - P4
-
Affects Version/s: None
-
Component/s: Serialization
-
None
If the object that is being inserted has a dynamic type with a JObject type in it (this is the default type used by JSON.NET for deserialize the JSON), then the InsertOneAsync method throws the StackOverflowException.
It would be OK if driver does not support JObject and throws an appropriate exception, but since the StackOverflowException crashes the application and is very hard to debug, at very least the driver should not cause this type of exception.
This is the code to replicate it:
async Task Main()
{
string msg = @"
";
dynamic details = Newtonsoft.Json.JsonConvert.DeserializeObject(msg);
var data = new DomainEventData()
;
MongoClient client = new MongoClient("mongodb://localhost/tests");
IMongoDatabase mongo = client.GetDatabase("tests");
IMongoCollection<DomainEventData> items = mongo.GetCollection<DomainEventData>("entries");
await items.InsertOneAsync(data);
}
// Define other methods and classes here
[Serializable]
public class DomainEventData
{
public Guid Id
[BsonElement("eventName")]
public string EventName { get; set; }
[BsonElement("tenantId")]
public Guid TenantId
[BsonElement("aggregateId")]
public Guid AggregateId { get; set; }
[BsonElement("aggregateName")]
public string AggregateName
[BsonElement("details")]
public dynamic Details { get; set; }
[BsonElement("utcTimestamp")]
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
public DateTime UtcTimestamp
public DomainEventData()
{ this.Id = Guid.NewGuid(); this.TenantId = Guid.Empty; this.AggregateId = Guid.Empty; this.EventName = string.Empty; this.UtcTimestamp = DateTime.UtcNow; this.AggregateName = string.Empty; }}
BTW, changing the:
dynamic details = Newtonsoft.Json.JsonConvert.DeserializeObject(msg);
to:
dynamic details = Newtonsoft.Json.JsonConvert.DeserializeObject<ExpandoObject>(msg);
solves the issue, but if application is dealing with unknown data coming in, this might not be an option.
- is related to
-
CSHARP-1385 Unable to explicitly set serializer for recursive enumerable types
- Closed
-
CSHARP-1281 Integrate with Json.NET
- Closed