elsa-core/src/modules/Elsa.MongoDb/Serializers/FlowScopeSerializer.cs
Sipke Schoorstra 1a554e7e06
Fix MongoDB serialization issues (#6762)
* Ongoing MongoDB work

* Refactor MongoDB serializer configuration and improve type handling

- Added `BsonSerializerHelpers` for streamlined serializer registration with error handling.
- Introduced `ConfigureMongoDbSerializers` hosted service to centralize serializer setup.
- Enhanced `FlowScopeSerializer` to handle additional BSON types.
- Cleaned up and reorganized MongoDB feature implementations, removing redundant code and improving consistency.

* Remove `BsonSerializerHelpers` and update MongoDB serializer registration

- Deleted `BsonSerializerHelpers` as it was redundant.
- Updated `ConfigureMongoDbSerializers` to directly register serializers using `BsonSerializer`.
- Simplified `FlowScopeSerializer` null handling for improved readability.

* Switch persistence provider to Entity Framework Core in `Program.cs`.

* Update src/modules/Elsa.MongoDb/Serializers/FlowScopeSerializer.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Configures MongoDB services and options.

Configures MongoDB services, including client and database creation.

Registers default naming strategy and collection naming strategy.

Adds BsonClassMap configuration for KeyValuePair.

* Simplify null check in `FlowScopeSerializer`.

* Allow `FlowScopeSerializer` to handle nullable `FlowScope`.

* Registers FlowScope BSON class map

Registers the `FlowScope` class with BSON to enable proper serialization and deserialization of workflow scopes within MongoDB.

This ensures that workflow scopes, which are used to manage variables within a workflow, are correctly stored and retrieved from the database.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-07-04 09:46:20 +02:00

52 lines
1.6 KiB
C#

using Elsa.Workflows;
using Elsa.Workflows.Activities.Flowchart.Models;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
namespace Elsa.MongoDb.Serializers;
/// <summary>
/// Serializes a <see cref="FlowScope"/>.
/// </summary>
public class FlowScopeSerializer(IPayloadSerializer payloadSerializer) : IBsonSerializer<FlowScope?>
{
/// <inheritdoc />
public Type ValueType => typeof(FlowScope);
void IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) => Serialize(context, args, (FlowScope)value);
object IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) => Deserialize(context, args);
/// <inheritdoc />
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, FlowScope? value)
{
if (value is null)
context.Writer.WriteNull();
else
{
var json = payloadSerializer.Serialize(value);
context.Writer.WriteString(json);
}
}
/// <inheritdoc />
public FlowScope Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var reader = context.Reader;
var bsonType = reader.GetCurrentBsonType();
if (bsonType == BsonType.Null)
{
reader.ReadNull();
return new();
}
if(bsonType == BsonType.String)
{
var json = context.Reader.ReadString();
return string.IsNullOrEmpty(json) ? new() : payloadSerializer.Deserialize<FlowScope>(json);
}
return new();
}
}