-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add configurable ObjectSerializer for MongoDB persistence to allow user type deserialization #1391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: danielgerlag <[email protected]>
…er types Co-authored-by: danielgerlag <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot this is only going to work because of the namespace of the samples, that will not be true in a real world use case.
…flowCore namespaces Co-authored-by: danielgerlag <[email protected]>
You're absolutely right. I've enhanced the fix to support real-world user types beyond just WorkflowCore namespaces. The updated ObjectSerializer registration now allows:
This works for user namespaces like Fixed in commit 70e02bb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot we should not make assumptions about namespaces. Remove all the hard coded namespace prefixes, and allow the user to configure what gets registered when they wire up Mongo as a persistence provider.
…ypeFilter parameter Co-authored-by: danielgerlag <[email protected]>
Done. Removed all hardcoded namespace checks and added a Users now explicitly configure the ObjectSerializer: services.AddWorkflow(x => x.UseMongoDB(
@"mongodb://localhost:27017",
"workflow",
serializerTypeFilter: type =>
ObjectSerializer.DefaultAllowedTypes(type) ||
type.FullName?.StartsWith("MyApp.") == true)); Updated README with documentation and examples. Fixed in commit 724de9f |
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.Solution
This fix adds a
serializerTypeFilter
parameter to theUseMongoDB
extension methods, allowing users to explicitly configure which types are allowed for serialization/deserialization. This approach makes no assumptions about user namespaces and gives complete control to the developer.Configuration Example:
Multiple Namespaces Example:
Key Benefits
Documentation
Updated README with comprehensive examples showing how to configure the ObjectSerializer for various scenarios.
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.