Skip to content

Commit

Permalink
take internals
Browse files Browse the repository at this point in the history
  • Loading branch information
Carael committed Nov 7, 2024
1 parent 94ed856 commit 2133cdf
Show file tree
Hide file tree
Showing 4 changed files with 608 additions and 143 deletions.
106 changes: 0 additions & 106 deletions src/Context/DefaultFrameworkAllowedTypes.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using MongoDB.Bson.Serialization;
using MongoDB.Extensions.Context.Extensions;
#nullable enable

namespace MongoDB.Extensions.Context.Internal;

public class TypeObjectSerializer : IBsonSerializer<object>
internal class TypeObjectSerializer : ObjectSerializer
{
private static readonly ConcurrentDictionary<Type, bool> _allowedTypes = new();
private static readonly HashSet<string> _allowedTypesByNamespaces = new();
private static readonly HashSet<string> _allowedTypesByDependencies = new();
private static readonly ConcurrentDictionary<Type, bool> _allowedTypes = new ();
private static readonly HashSet<string> _allowedTypesByNamespaces = new ();
private static readonly HashSet<string> _allowedTypesByDependencies = new ();

public Type ValueType => typeof(object);
public TypeObjectSerializer() : base(type => IsTypeAllowed(type))
{
}

public static IReadOnlyDictionary<Type, bool> AllowedTypes => _allowedTypes;
public static IReadOnlyDictionary<Type, bool> AllowedTypes
=> _allowedTypes;

public static IReadOnlyCollection<string> AllowedTypesByNamespaces => _allowedTypesByNamespaces;
public static IReadOnlyCollection<string> AllowedTypesByNamespaces
=> _allowedTypesByNamespaces;

public static IReadOnlyCollection<string> AllowedTypesByDependencies => _allowedTypesByDependencies;
public static IReadOnlyCollection<string> AllowedTypesByDependencies
=> _allowedTypesByDependencies;

public static bool IsTypeAllowed(Type type)
{
return DefaultAllowedTypes(type) ||
_allowedTypes.ContainsKey(type) ||
IsInAllowedNamespaces(type) ||
IsInAllowedDependencyTypes(type);
_allowedTypes.ContainsKey(type) ||
IsInAllowedNamespaces(type) ||
IsInAllowedDependencyTypes(type);
}

public static Func<Type, bool> DefaultAllowedTypes => DefaultFrameworkAllowedTypes.AllowedTypes;

public static void AddAllowedType<T>()
{
_allowedTypes.TryAdd(typeof(T), true);
Expand Down Expand Up @@ -85,12 +88,13 @@ private static bool IsAllowedNameSpacePart(Type type)
{
foreach (string allowedNamespace in _allowedTypesByNamespaces)
{
if (string.IsNullOrEmpty(type.Namespace))
if(string.IsNullOrEmpty(type.Namespace))
{
return false;
}

if (type.Namespace.StartsWith(allowedNamespace, StringComparison.OrdinalIgnoreCase))
if (type.Namespace.StartsWith(allowedNamespace,
StringComparison.OrdinalIgnoreCase))
{
return true;
}
Expand All @@ -101,30 +105,11 @@ private static bool IsAllowedNameSpacePart(Type type)

private static bool IsInAllowedDependencyTypes(Type type)
{
bool isInDependencyTypes = _allowedTypesByDependencies.Contains(type.GetRootNamespace());
bool isInDependencyTypes = _allowedTypesByDependencies
.Contains(type.GetRootNamespace());

_allowedTypes.TryAdd(type, isInDependencyTypes);

return isInDependencyTypes;
}

public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
{
var serializer = BsonSerializer.LookupSerializer(value.GetType());
serializer.Serialize(context, args, value);
}

public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var bsonType = context.Reader.GetCurrentBsonType();
if (bsonType == MongoDB.Bson.BsonType.Null)
{
context.Reader.ReadNull();
return null!;
}

var type = args.NominalType;
var serializer = BsonSerializer.LookupSerializer(type);
return serializer.Deserialize(context, args);
}
}
Loading

0 comments on commit 2133cdf

Please sign in to comment.