Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Context.AllowedTypes.Tests/Helpers/TestHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using MongoDB.Extensions.Context.Internal;

namespace MongoDB.Extensions.Context.AllowedTypes.Tests.Helpers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using MongoDB.Extensions.Context.AllowedTypes.Tests.Helpers;
using MongoDB.Extensions.Context.Internal;
using Snapshooter.Xunit;
using Squadron;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MongoDB.Extensions.Context.AllowedTypes.Tests.Helpers;
using MongoDB.Extensions.Context.Internal;
using Snapshooter.Xunit;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(CCTestProjectProps)" Condition="Exists('$(CCTestProjectProps)')" />

<PropertyGroup>
<AssemblyName>MongoDB.Extensions.Context.GuidSerializers.Tests</AssemblyName>
<RootNamespace>MongoDB.Extensions.Context.GuidSerializers.Tests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Context\Context.csproj" />
<ProjectReference Include="..\Prime.Extensions\Prime.Extensions.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Xunit.Priority" />
</ItemGroup>

</Project>
71 changes: 71 additions & 0 deletions src/Context.GuidSerializer.Tests/GuidSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using MongoDB.Driver;
using Squadron;
using Xunit;
using System;
using System.Threading.Tasks;
using Snapshooter.Xunit;
using MongoDB.Prime.Extensions;

namespace MongoDB.Extensions.Context.GuidSerializers.Tests;

public class GuidSerializerTests : IClassFixture<MongoResource>
{
private readonly MongoOptions _mongoOptions;
private readonly IMongoDatabase _mongoDatabase;

public GuidSerializerTests(MongoResource mongoResource)
{
_mongoDatabase = mongoResource.CreateDatabase();
_mongoOptions = new MongoOptions
{
ConnectionString = mongoResource.ConnectionString,
DatabaseName = _mongoDatabase.DatabaseNamespace.DatabaseName
};
}

[Fact]
public async Task Serialize_GuidPropertyGuidSerialized_Successfully()
{
// Arrange
var foobarMongoDbContext = new FooBarMongoDbContext(_mongoOptions);

IMongoCollection<Foo> collection =
_mongoDatabase.GetCollection<Foo>("foos");

Foo foo = new Foo
(
fooId: Guid.Parse("b1eba0d6-a1f9-4e31-bd70-0feed19f4492"),
name: "test",
additionalId: Guid.Parse("b58ec857-c874-457e-8662-133a055282f6")
);

// Act
await collection.InsertOneAsync(foo);

// Assert
Snapshot.Match(collection.Dump());
}

[Fact]
public async Task Serialize_ObjectPropertyGuidSerialized_Successfully()
{
// Arrange
var foobarMongoDbContext = new FooBarMongoDbContext(_mongoOptions);

IMongoCollection<Bar> collection =
_mongoDatabase.GetCollection<Bar>("bars");

Bar bar = new Bar
(
fooId: Guid.Parse("b1eba0d6-a1f9-4e31-bd70-0feed19f4492"),
name: "test",
additionalId: Guid.Parse("b58ec857-c874-457e-8662-133a055282f6")
);

// Act
await collection.InsertOneAsync(bar);

// Assert
Snapshot.Match(collection.Dump());
}
}
19 changes: 19 additions & 0 deletions src/Context.GuidSerializer.Tests/Helpers/Bar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace MongoDB.Extensions.Context.GuidSerializers.Tests;

public class Bar
{
public Bar(Guid fooId, string name, Guid additionalId)
{
Id = fooId;
Name = name;
AdditionalId = additionalId;
}

public Guid Id { get; private set; }

public string Name { get; private set;}

public object AdditionalId { get; private set;}
}
19 changes: 19 additions & 0 deletions src/Context.GuidSerializer.Tests/Helpers/Foo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace MongoDB.Extensions.Context.GuidSerializers.Tests;

public class Foo
{
public Foo(Guid fooId, string name, Guid additionalId)
{
Id = fooId;
Name = name;
AdditionalId = additionalId;
}

public Guid Id { get; private set; }

public string Name { get; private set;}

public Guid AdditionalId { get; private set;}
}
18 changes: 18 additions & 0 deletions src/Context.GuidSerializer.Tests/Helpers/FooBarMongoDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Serializers;

namespace MongoDB.Extensions.Context.GuidSerializers.Tests;

public class FooBarMongoDbContext : MongoDbContext
{
public FooBarMongoDbContext(MongoOptions mongoOptions)
: base(mongoOptions)
{
}

protected override void OnConfiguring(
IMongoDatabaseBuilder databaseBuilder)
{
databaseBuilder.RegisterSerializer(new GuidSerializer(GuidRepresentation.CSharpLegacy));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"Id": "b1eba0d6-a1f9-4e31-bd70-0feed19f4492",
"Name": "test",
"AdditionalId": "b58ec857-c874-457e-8662-133a055282f6"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"Id": "b1eba0d6-a1f9-4e31-bd70-0feed19f4492",
"Name": "test",
"AdditionalId": "b58ec857-c874-457e-8662-133a055282f6"
}
]
35 changes: 28 additions & 7 deletions src/Context/Internal/TypeObjectSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Extensions.Context.Extensions;

#nullable enable

namespace MongoDB.Extensions.Context.Internal;
namespace MongoDB.Extensions.Context;

internal class TypeObjectSerializer : ClassSerializerBase<object>, IHasDiscriminatorConvention
public class TypeObjectSerializer : ClassSerializerBase<object>, IHasDiscriminatorConvention
{
private readonly ObjectSerializer _objectSerializer;
private static readonly Dictionary<Type, bool> _allowedTypes = new();
private static readonly HashSet<string> _allowedTypesByNamespaces = new();
private static readonly HashSet<string> _allowedTypesByDependencies = new();
private static readonly object _lock = new();

public TypeObjectSerializer()
public TypeObjectSerializer(ObjectSerializer? objectSerializer = null)
{
_objectSerializer = new ObjectSerializer(type => IsTypeAllowed(type));
_objectSerializer = objectSerializer ?? CreateObjectSerializer();

DiscriminatorConvention = _objectSerializer.GetDiscriminatorConvention();
}

Expand All @@ -47,7 +48,7 @@ public static bool IsTypeAllowed(Type type)
public static void AddAllowedType<T>()
{
lock (_lock)
{
{
_allowedTypes.Add(typeof(T), true);
}
}
Expand All @@ -67,7 +68,7 @@ public static void AddAllowedTypes(params string[] allowedNamespaces)
{
lock (_lock)
{
foreach (string allowedNamespace in allowedNamespaces)
foreach (var allowedNamespace in allowedNamespaces)
{
_allowedTypesByNamespaces.Add(allowedNamespace);
}
Expand Down Expand Up @@ -160,4 +161,24 @@ public override int GetHashCode()
{
return _objectSerializer.GetHashCode();
}

private ObjectSerializer CreateObjectSerializer()
{
IDiscriminatorConvention objectDiscriminatorConvention =
BsonSerializer.LookupDiscriminatorConvention(typeof(object));

var serializer =
BsonSerializer.LookupSerializer(typeof(Guid)) as GuidSerializer;

GuidRepresentation guidRepresentation =
serializer?.GuidRepresentation ?? GuidRepresentation.Unspecified;

var objectSerializer =
new ObjectSerializer(
objectDiscriminatorConvention,
guidRepresentation,
type => IsTypeAllowed(type));

return objectSerializer;
}
}
6 changes: 6 additions & 0 deletions src/MongoDB.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Migration", "Migration\Migr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Migration.Tests", "Migration.Tests\Migration.Tests.csproj", "{D8F246AC-65CC-4EF0-B058-08970D269B61}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Context.GuidSerializers.Tests", "Context.GuidSerializer.Tests\Context.GuidSerializers.Tests.csproj", "{2E5C44AC-9F56-462F-B0B7-25F5995F5B76}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -93,6 +95,10 @@ Global
{D8F246AC-65CC-4EF0-B058-08970D269B61}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8F246AC-65CC-4EF0-B058-08970D269B61}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8F246AC-65CC-4EF0-B058-08970D269B61}.Release|Any CPU.Build.0 = Release|Any CPU
{2E5C44AC-9F56-462F-B0B7-25F5995F5B76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E5C44AC-9F56-462F-B0B7-25F5995F5B76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E5C44AC-9F56-462F-B0B7-25F5995F5B76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E5C44AC-9F56-462F-B0B7-25F5995F5B76}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading