Skip to content

Commit b110382

Browse files
committed
fix serialization issue with MongoDB provider
1 parent 7760f1e commit b110382

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/providers/WorkflowCore.Persistence.MongoDB/Services/DataObjectSerializer.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class DataObjectSerializer : SerializerBase<object>
1616
{
1717
private static JsonSerializerSettings SerializerSettings = new JsonSerializerSettings()
1818
{
19-
TypeNameHandling = TypeNameHandling.All
19+
TypeNameHandling = TypeNameHandling.Objects,
2020
};
2121

2222
public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
@@ -32,15 +32,45 @@ public override object Deserialize(BsonDeserializationContext context, BsonDeser
3232

3333
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
3434
{
35-
string str = JsonConvert.SerializeObject(value, SerializerSettings);
35+
var str = JsonConvert.SerializeObject(value, SerializerSettings);
3636
var doc = BsonDocument.Parse(str);
37-
var typeElem = doc.GetElement("$type");
38-
doc.RemoveElement(typeElem);
39-
40-
if (doc.Elements.All(x => x.Name != "_t"))
41-
doc.InsertAt(0, new BsonElement("_t", typeElem.Value));
37+
ConvertMetaFormat(doc);
4238

4339
BsonSerializer.Serialize(context.Writer, doc);
4440
}
41+
42+
private static void ConvertMetaFormat(BsonDocument root)
43+
{
44+
var stack = new Stack<BsonDocument>();
45+
stack.Push(root);
46+
47+
while (stack.Count > 0)
48+
{
49+
var doc = stack.Pop();
50+
51+
if (doc.TryGetElement("$type", out var typeElem))
52+
{
53+
doc.RemoveElement(typeElem);
54+
55+
if (doc.Elements.All(x => x.Name != "_t"))
56+
doc.InsertAt(0, new BsonElement("_t", typeElem.Value));
57+
}
58+
59+
foreach (var subDoc in doc.Elements)
60+
{
61+
if (subDoc.Value.IsBsonDocument)
62+
stack.Push(subDoc.Value.ToBsonDocument());
63+
64+
if (subDoc.Value.IsBsonArray)
65+
{
66+
foreach (var element in subDoc.Value.AsBsonArray)
67+
{
68+
if (element.IsBsonDocument)
69+
stack.Push(element.ToBsonDocument());
70+
}
71+
}
72+
}
73+
}
74+
}
4575
}
4676
}

src/providers/WorkflowCore.Persistence.MongoDB/WorkflowCore.Persistence.MongoDB.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1818
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
20-
<Version>1.6.0</Version>
20+
<Version>1.6.2</Version>
2121
<Description>Provides support to persist workflows running on Workflow Core to a MongoDB database.</Description>
22-
<AssemblyVersion>1.6.0.0</AssemblyVersion>
23-
<FileVersion>1.6.0.0</FileVersion>
22+
<AssemblyVersion>1.6.2.0</AssemblyVersion>
23+
<FileVersion>1.6.2.0</FileVersion>
2424
</PropertyGroup>
2525

2626
<ItemGroup>

0 commit comments

Comments
 (0)