Skip to content

Commit 2133cdf

Browse files
committed
take internals
1 parent 94ed856 commit 2133cdf

File tree

4 files changed

+608
-143
lines changed

4 files changed

+608
-143
lines changed

src/Context/DefaultFrameworkAllowedTypes.cs

Lines changed: 0 additions & 106 deletions
This file was deleted.

src/Context/TypeObjectSerializer.cs renamed to src/Context/Internal/TypeObjectSerializer.cs

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4-
using MongoDB.Bson.Serialization;
54
using MongoDB.Extensions.Context.Extensions;
5+
#nullable enable
66

77
namespace MongoDB.Extensions.Context.Internal;
88

9-
public class TypeObjectSerializer : IBsonSerializer<object>
9+
internal class TypeObjectSerializer : ObjectSerializer
1010
{
11-
private static readonly ConcurrentDictionary<Type, bool> _allowedTypes = new();
12-
private static readonly HashSet<string> _allowedTypesByNamespaces = new();
13-
private static readonly HashSet<string> _allowedTypesByDependencies = new();
11+
private static readonly ConcurrentDictionary<Type, bool> _allowedTypes = new ();
12+
private static readonly HashSet<string> _allowedTypesByNamespaces = new ();
13+
private static readonly HashSet<string> _allowedTypesByDependencies = new ();
1414

15-
public Type ValueType => typeof(object);
15+
public TypeObjectSerializer() : base(type => IsTypeAllowed(type))
16+
{
17+
}
1618

17-
public static IReadOnlyDictionary<Type, bool> AllowedTypes => _allowedTypes;
19+
public static IReadOnlyDictionary<Type, bool> AllowedTypes
20+
=> _allowedTypes;
1821

19-
public static IReadOnlyCollection<string> AllowedTypesByNamespaces => _allowedTypesByNamespaces;
22+
public static IReadOnlyCollection<string> AllowedTypesByNamespaces
23+
=> _allowedTypesByNamespaces;
2024

21-
public static IReadOnlyCollection<string> AllowedTypesByDependencies => _allowedTypesByDependencies;
25+
public static IReadOnlyCollection<string> AllowedTypesByDependencies
26+
=> _allowedTypesByDependencies;
2227

2328
public static bool IsTypeAllowed(Type type)
2429
{
2530
return DefaultAllowedTypes(type) ||
26-
_allowedTypes.ContainsKey(type) ||
27-
IsInAllowedNamespaces(type) ||
28-
IsInAllowedDependencyTypes(type);
31+
_allowedTypes.ContainsKey(type) ||
32+
IsInAllowedNamespaces(type) ||
33+
IsInAllowedDependencyTypes(type);
2934
}
3035

31-
public static Func<Type, bool> DefaultAllowedTypes => DefaultFrameworkAllowedTypes.AllowedTypes;
32-
3336
public static void AddAllowedType<T>()
3437
{
3538
_allowedTypes.TryAdd(typeof(T), true);
@@ -85,12 +88,13 @@ private static bool IsAllowedNameSpacePart(Type type)
8588
{
8689
foreach (string allowedNamespace in _allowedTypesByNamespaces)
8790
{
88-
if (string.IsNullOrEmpty(type.Namespace))
91+
if(string.IsNullOrEmpty(type.Namespace))
8992
{
9093
return false;
9194
}
9295

93-
if (type.Namespace.StartsWith(allowedNamespace, StringComparison.OrdinalIgnoreCase))
96+
if (type.Namespace.StartsWith(allowedNamespace,
97+
StringComparison.OrdinalIgnoreCase))
9498
{
9599
return true;
96100
}
@@ -101,30 +105,11 @@ private static bool IsAllowedNameSpacePart(Type type)
101105

102106
private static bool IsInAllowedDependencyTypes(Type type)
103107
{
104-
bool isInDependencyTypes = _allowedTypesByDependencies.Contains(type.GetRootNamespace());
108+
bool isInDependencyTypes = _allowedTypesByDependencies
109+
.Contains(type.GetRootNamespace());
105110

106111
_allowedTypes.TryAdd(type, isInDependencyTypes);
107112

108113
return isInDependencyTypes;
109114
}
110-
111-
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
112-
{
113-
var serializer = BsonSerializer.LookupSerializer(value.GetType());
114-
serializer.Serialize(context, args, value);
115-
}
116-
117-
public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
118-
{
119-
var bsonType = context.Reader.GetCurrentBsonType();
120-
if (bsonType == MongoDB.Bson.BsonType.Null)
121-
{
122-
context.Reader.ReadNull();
123-
return null!;
124-
}
125-
126-
var type = args.NominalType;
127-
var serializer = BsonSerializer.LookupSerializer(type);
128-
return serializer.Deserialize(context, args);
129-
}
130115
}

0 commit comments

Comments
 (0)