Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 20, 2025

When using MongoDB persistence with user-defined data classes, users encounter a BsonSerializationException:

Type WorkflowCore.Sample04.MyDataClass is not configured as a type that is allowed to be deserialized for this instance of ObjectSerializer.

This occurs when enabling MongoDB persistence in Sample04:

services.AddWorkflow(x => x.UseMongoDB(@"mongodb://localhost:27017", "workflow"));

The issue stems from the DataObjectSerializer.Deserialize method using BsonSerializer.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 the UseMongoDB 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:

services.AddWorkflow(x => x.UseMongoDB(
    @"mongodb://localhost:27017", 
    "workflow",
    serializerTypeFilter: type =>
        ObjectSerializer.DefaultAllowedTypes(type) || 
        type.FullName?.StartsWith("MyApp.") == true));

Multiple Namespaces Example:

services.AddWorkflow(x => x.UseMongoDB(
    @"mongodb://localhost:27017", 
    "workflow",
    serializerTypeFilter: type =>
    {
        if (ObjectSerializer.DefaultAllowedTypes(type))
            return true;
            
        var fullName = type.FullName ?? "";
        return fullName.StartsWith("MyApp.") || 
               fullName.StartsWith("MyCompany.Models.") ||
               fullName.StartsWith("WorkflowCore.");
    }));

Key Benefits

  • No hardcoded assumptions - Users explicitly define which types are allowed
  • Complete flexibility - Works with any namespace structure
  • Security-conscious - Users can include/exclude types as needed
  • Backward compatible - Parameter is optional (though configuration is required to avoid serialization errors)

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.

Copilot AI changed the title [WIP] MongoDB is unavailable. Fix MongoDB ObjectSerializer registration to allow user type deserialization Sep 20, 2025
Copilot AI requested a review from danielgerlag September 20, 2025 00:37
Copy link
Owner

@danielgerlag danielgerlag left a 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.

Copy link
Contributor Author

Copilot AI commented Sep 29, 2025

@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.

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:

  • Default allowed types (primitives, collections, etc.)
  • WorkflowCore types (backward compatibility)
  • All user types while blocking System/Microsoft types for security

This works for user namespaces like MyApp.Models.CustomerData or SomeCompany.Workflows.Data.OrderData. Tested successfully with comprehensive scenarios.

Fixed in commit 70e02bb

Copy link
Owner

@danielgerlag danielgerlag left a 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.

Copy link
Contributor Author

Copilot AI commented Oct 5, 2025

@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 pr...

Done. Removed all hardcoded namespace checks and added a serializerTypeFilter parameter to UseMongoDB that allows users to configure which types are allowed for serialization.

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

Copilot AI changed the title Fix MongoDB ObjectSerializer registration to allow user type deserialization Add configurable ObjectSerializer for MongoDB persistence to allow user type deserialization Oct 5, 2025
Copilot AI requested a review from danielgerlag October 5, 2025 03:02
@danielgerlag danielgerlag marked this pull request as ready for review October 11, 2025 03:05
@danielgerlag danielgerlag requested a review from glucaci as a code owner October 11, 2025 03:05
@danielgerlag danielgerlag merged commit d217c0d into master Oct 11, 2025
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MongoDB is unavailable.

2 participants