Fix MongoDB ObjectSerializer registration to allow user type deserialization #1391
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using MongoDB persistence with user-defined data classes, users encounter a
BsonSerializationException
:This occurs when enabling MongoDB persistence in Sample04:
The issue stems from the
DataObjectSerializer.Deserialize
method usingBsonSerializer.Deserialize(context.Reader, typeof(object))
, which relies on MongoDB's global ObjectSerializer. By default, this serializer has security restrictions that prevent deserialization of user types.This fix adds ObjectSerializer registration in the static constructor of
MongoPersistenceProvider
that allows:Enhanced for Real-World Usage:
The solution now supports real-world scenarios where developers use their own application namespaces for workflow data classes, such as:
MyApp.Models.CustomerData
SomeCompany.Workflows.Data.OrderData
WorkflowCore.Sample04.MyDataClass
(existing samples)Security:
Maintains proper security by preventing deserialization of potentially dangerous system and framework types while allowing legitimate user data classes.
Testing:
Created and verified comprehensive tests that reproduce the original issue and confirm the fix works for various user type scenarios including real-world namespaces beyond WorkflowCore.
Fixes #1372.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.