From 2e457ee46345bf396dca1a48b452e065e99a3966 Mon Sep 17 00:00:00 2001 From: Mariusz Matysek Date: Wed, 20 Nov 2024 11:12:41 +0100 Subject: [PATCH] Removed mongo internals --- src/Context/Internal/TypeObjectSerializer.cs | 2 +- .../MongoDriverInternals/ObjectSerializer.cs | 120 ------------------ .../MongoDriverInternals/TypeExtensions.cs | 17 --- 3 files changed, 1 insertion(+), 138 deletions(-) delete mode 100644 src/Context/MongoDriverInternals/ObjectSerializer.cs delete mode 100644 src/Context/MongoDriverInternals/TypeExtensions.cs diff --git a/src/Context/Internal/TypeObjectSerializer.cs b/src/Context/Internal/TypeObjectSerializer.cs index d61e790..bdb913e 100644 --- a/src/Context/Internal/TypeObjectSerializer.cs +++ b/src/Context/Internal/TypeObjectSerializer.cs @@ -34,7 +34,7 @@ public static IReadOnlyCollection AllowedTypesByDependencies public static bool IsTypeAllowed(Type type) { - return DefaultFrameworkAllowedTypes.AllowedTypes(type) || + return ObjectSerializer.DefaultAllowedTypes.Invoke(type) || _allowedTypes.ContainsKey(type) || IsInAllowedNamespaces(type) || IsInAllowedDependencyTypes(type); diff --git a/src/Context/MongoDriverInternals/ObjectSerializer.cs b/src/Context/MongoDriverInternals/ObjectSerializer.cs deleted file mode 100644 index 04e83d8..0000000 --- a/src/Context/MongoDriverInternals/ObjectSerializer.cs +++ /dev/null @@ -1,120 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using MongoDB.Bson; - -namespace MongoDB.Extensions.Context; - -/* Copyright 2010-present MongoDB Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -public static class DefaultFrameworkAllowedTypes -{ - private readonly static Func __allowedTypes = AllowedTypesImplementation; - - private readonly static HashSet __allowedNonGenericTypesSet = new HashSet - { - typeof(bool), - typeof(byte), - typeof(char), - typeof(System.Collections.ArrayList), - typeof(System.Collections.BitArray), - typeof(System.Collections.Hashtable), - typeof(System.Collections.Queue), - typeof(System.Collections.SortedList), - typeof(System.Collections.Specialized.ListDictionary), - typeof(System.Collections.Specialized.OrderedDictionary), - typeof(System.Collections.Stack), - typeof(System.DateTime), - typeof(System.DateTimeOffset), - typeof(decimal), - typeof(double), - typeof(System.Dynamic.ExpandoObject), - typeof(System.Guid), - typeof(short), - typeof(int), - typeof(long), - typeof(System.Net.DnsEndPoint), - typeof(System.Net.EndPoint), - typeof(System.Net.IPAddress), - typeof(System.Net.IPEndPoint), - typeof(System.Net.IPHostEntry), - typeof(object), - typeof(sbyte), - typeof(float), - typeof(string), - typeof(System.Text.RegularExpressions.Regex), - typeof(System.TimeSpan), - typeof(ushort), - typeof(uint), - typeof(ulong), - typeof(System.Uri), - typeof(System.Version) - }; - - private readonly static HashSet __allowedGenericTypesSet = new HashSet - { - typeof(System.Collections.Generic.Dictionary<,>), - typeof(System.Collections.Generic.HashSet<>), - typeof(System.Collections.Generic.KeyValuePair<,>), - typeof(System.Collections.Generic.LinkedList<>), - typeof(System.Collections.Generic.List<>), - typeof(System.Collections.Generic.Queue<>), - typeof(System.Collections.Generic.SortedDictionary<,>), - typeof(System.Collections.Generic.SortedList<,>), - typeof(System.Collections.Generic.SortedSet<>), - typeof(System.Collections.Generic.Stack<>), - typeof(System.Collections.ObjectModel.Collection<>), - typeof(System.Collections.ObjectModel.KeyedCollection<,>), - typeof(System.Collections.ObjectModel.ObservableCollection<>), - typeof(System.Collections.ObjectModel.ReadOnlyCollection<>), - typeof(System.Collections.ObjectModel.ReadOnlyDictionary<,>), - typeof(System.Collections.ObjectModel.ReadOnlyObservableCollection<>), - typeof(System.Nullable<>), - typeof(System.Tuple<>), - typeof(System.Tuple<,>), - typeof(System.Tuple<,,>), - typeof(System.Tuple<,,,>), - typeof(System.Tuple<,,,,>), - typeof(System.Tuple<,,,,,>), - typeof(System.Tuple<,,,,,,>), - typeof(System.Tuple<,,,,,,,>), - typeof(System.ValueTuple<,,,,,,,>), - typeof(System.ValueTuple<>), - typeof(System.ValueTuple<,>), - typeof(System.ValueTuple<,,>), - typeof(System.ValueTuple<,,,>), - typeof(System.ValueTuple<,,,,>), - typeof(System.ValueTuple<,,,,,>), - typeof(System.ValueTuple<,,,,,,>), - typeof(System.ValueTuple<,,,,,,,>) - }; - - public static Func AllowedTypes => __allowedTypes; - - private static bool AllowedTypesImplementation(Type type) - { - return type.IsConstructedGenericType ? IsAllowedGenericType(type) : IsAllowedType(type); - - static bool IsAllowedType(Type type) => - typeof(BsonValue).IsAssignableFrom(type) || - __allowedNonGenericTypesSet.Contains(type) || - type.IsArray && AllowedTypesImplementation(type.GetElementType()) || - type.IsEnum; - - static bool IsAllowedGenericType(Type type) => - (__allowedGenericTypesSet.Contains(type.GetGenericTypeDefinition()) || type.IsAnonymousType()) && - type.GetGenericArguments().All(__allowedTypes); - } -} diff --git a/src/Context/MongoDriverInternals/TypeExtensions.cs b/src/Context/MongoDriverInternals/TypeExtensions.cs deleted file mode 100644 index a1f2202..0000000 --- a/src/Context/MongoDriverInternals/TypeExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Linq; -using System.Runtime.CompilerServices; - -namespace MongoDB.Extensions.Context; - -internal static class TypeExtensions -{ - public static bool IsAnonymousType(this Type type) - { - // don't test for too many things in case implementation details change in the future - return - type.GetCustomAttributes(false).Any(x => x is CompilerGeneratedAttribute) && - type.IsGenericType && - type.Name.Contains("Anon"); // don't check for more than "Anon" so it works in mono also - } -} \ No newline at end of file