From 5c93348b27ccc277f1af5eaac42d89c5e29fd29d Mon Sep 17 00:00:00 2001 From: Normen Scheiber Date: Wed, 22 Nov 2023 11:56:54 +0100 Subject: [PATCH 1/4] Bug fix for AddAllowedTypes by namespace --- .../Context.AllowedTypes.Tests.csproj | 4 +++ .../TypeObjectSerializerTests.cs | 32 ++++++++++++++++++- ...AllowedNamespacesCaseInsensitive_True.snap | 12 +++++++ ...lowed_PartIsInAllowedNamespaces_True.snap} | 2 +- ...ed_PartIsNotInAllowedNamespaces_False.snap | 12 +++++++ src/Context/TypeObjectSerializer.cs | 21 +++++++++--- 6 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsInAllowedNamespacesCaseInsensitive_True.snap rename src/Context.AllowedTypes.Tests/__snapshots__/{TypeObjectSerializerTests.IsTypeAllowed_InAllowedNamespaces_False.snap => TypeObjectSerializerTests.IsTypeAllowed_PartIsInAllowedNamespaces_True.snap} (91%) create mode 100644 src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsNotInAllowedNamespaces_False.snap diff --git a/src/Context.AllowedTypes.Tests/Context.AllowedTypes.Tests.csproj b/src/Context.AllowedTypes.Tests/Context.AllowedTypes.Tests.csproj index ae481c1..28001f2 100644 --- a/src/Context.AllowedTypes.Tests/Context.AllowedTypes.Tests.csproj +++ b/src/Context.AllowedTypes.Tests/Context.AllowedTypes.Tests.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/src/Context.AllowedTypes.Tests/TypeObjectSerializerTests.cs b/src/Context.AllowedTypes.Tests/TypeObjectSerializerTests.cs index a8b0735..4732c88 100644 --- a/src/Context.AllowedTypes.Tests/TypeObjectSerializerTests.cs +++ b/src/Context.AllowedTypes.Tests/TypeObjectSerializerTests.cs @@ -94,7 +94,7 @@ public void IsTypeAllowed_InAllowedNamespaces_True() } [Fact] - public void IsTypeAllowed_InAllowedNamespaces_False() + public void IsTypeAllowed_PartIsInAllowedNamespaces_True() { // Arrange TypeObjectSerializer.Clear(); @@ -103,6 +103,36 @@ public void IsTypeAllowed_InAllowedNamespaces_False() // Act bool isAllowed = TypeObjectSerializer.IsTypeAllowed(typeof(Foo)); + // Assert + Assert.True(isAllowed); + Snapshot.Match(TestHelpers.GetTypeObjectSerializerContent()); + } + + [Fact] + public void IsTypeAllowed_PartIsInAllowedNamespacesCaseInsensitive_True() + { + // Arrange + TypeObjectSerializer.Clear(); + TypeObjectSerializer.AddAllowedTypes("MONGODB.EXTENSIONS"); + + // Act + bool isAllowed = TypeObjectSerializer.IsTypeAllowed(typeof(Foo)); + + // Assert + Assert.True(isAllowed); + Snapshot.Match(TestHelpers.GetTypeObjectSerializerContent()); + } + + [Fact] + public void IsTypeAllowed_PartIsNotInAllowedNamespaces_False() + { + // Arrange + TypeObjectSerializer.Clear(); + TypeObjectSerializer.AddAllowedTypes("MongoDBs.Context"); + + // Act + bool isAllowed = TypeObjectSerializer.IsTypeAllowed(typeof(Foo)); + // Assert Assert.False(isAllowed); Snapshot.Match(TestHelpers.GetTypeObjectSerializerContent()); diff --git a/src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsInAllowedNamespacesCaseInsensitive_True.snap b/src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsInAllowedNamespacesCaseInsensitive_True.snap new file mode 100644 index 0000000..2822e2a --- /dev/null +++ b/src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsInAllowedNamespacesCaseInsensitive_True.snap @@ -0,0 +1,12 @@ +{ + "AllowedTypes": [ + { + "Key": "MongoDB.Extensions.Context.AllowedTypes.Tests.Foo", + "Value": true + } + ], + "AllowedTypesByNamespaces": [ + "MONGODB.EXTENSIONS" + ], + "AllowedTypesByDependencies": [] +} diff --git a/src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_InAllowedNamespaces_False.snap b/src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsInAllowedNamespaces_True.snap similarity index 91% rename from src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_InAllowedNamespaces_False.snap rename to src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsInAllowedNamespaces_True.snap index 5b87fd2..61efd17 100644 --- a/src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_InAllowedNamespaces_False.snap +++ b/src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsInAllowedNamespaces_True.snap @@ -2,7 +2,7 @@ "AllowedTypes": [ { "Key": "MongoDB.Extensions.Context.AllowedTypes.Tests.Foo", - "Value": false + "Value": true } ], "AllowedTypesByNamespaces": [ diff --git a/src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsNotInAllowedNamespaces_False.snap b/src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsNotInAllowedNamespaces_False.snap new file mode 100644 index 0000000..133c82e --- /dev/null +++ b/src/Context.AllowedTypes.Tests/__snapshots__/TypeObjectSerializerTests.IsTypeAllowed_PartIsNotInAllowedNamespaces_False.snap @@ -0,0 +1,12 @@ +{ + "AllowedTypes": [ + { + "Key": "MongoDB.Extensions.Context.AllowedTypes.Tests.Foo", + "Value": false + } + ], + "AllowedTypesByNamespaces": [ + "MongoDBs.Context" + ], + "AllowedTypesByDependencies": [] +} diff --git a/src/Context/TypeObjectSerializer.cs b/src/Context/TypeObjectSerializer.cs index bc6cd5a..7f91065 100644 --- a/src/Context/TypeObjectSerializer.cs +++ b/src/Context/TypeObjectSerializer.cs @@ -70,15 +70,14 @@ internal static void Clear() private static bool IsInAllowedNamespaces(Type type) { - if(type.Namespace is null) + if (type.Namespace is null) { return false; } - bool isInAllowedNamespaces = _allowedTypesByNamespaces - .Contains(type.Namespace); + var isInAllowedNamespaces = IsAllowedNameSpacePart(type); - if(isInAllowedNamespaces) + if (isInAllowedNamespaces) { _allowedTypes.TryAdd(type, true); } @@ -86,6 +85,20 @@ private static bool IsInAllowedNamespaces(Type type) return isInAllowedNamespaces; } + private static bool IsAllowedNameSpacePart(Type type) + { + foreach (string allowedNamespace in _allowedTypesByNamespaces) + { + if (type.Namespace.StartsWith(allowedNamespace, + StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + + return false; + } + private static bool IsInAllowedDependencyTypes(Type type) { bool isInDependencyTypes = _allowedTypesByDependencies From fc1050618ed4d409fd1855aa470a79a222371875 Mon Sep 17 00:00:00 2001 From: Normen Scheiber Date: Wed, 22 Nov 2023 11:58:21 +0100 Subject: [PATCH 2/4] fix proj --- .../Context.AllowedTypes.Tests.csproj | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Context.AllowedTypes.Tests/Context.AllowedTypes.Tests.csproj b/src/Context.AllowedTypes.Tests/Context.AllowedTypes.Tests.csproj index 28001f2..192045e 100644 --- a/src/Context.AllowedTypes.Tests/Context.AllowedTypes.Tests.csproj +++ b/src/Context.AllowedTypes.Tests/Context.AllowedTypes.Tests.csproj @@ -10,9 +10,5 @@ - - - - - + From 736c2b38e970165c3a74580befc89468ebfa6ac2 Mon Sep 17 00:00:00 2001 From: Normen Scheiber Date: Fri, 19 Jan 2024 10:53:04 +0100 Subject: [PATCH 3/4] Fixed code smell. --- src/Context/TypeObjectSerializer.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Context/TypeObjectSerializer.cs b/src/Context/TypeObjectSerializer.cs index 7f91065..14db994 100644 --- a/src/Context/TypeObjectSerializer.cs +++ b/src/Context/TypeObjectSerializer.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Linq; using MongoDB.Bson.Serialization.Serializers; using MongoDB.Extensions.Context.Extensions; +#nullable enable namespace MongoDB.Extensions.Context.Internal; @@ -89,6 +89,11 @@ private static bool IsAllowedNameSpacePart(Type type) { foreach (string allowedNamespace in _allowedTypesByNamespaces) { + if(string.IsNullOrEmpty(type.Namespace)) + { + return false; + } + if (type.Namespace.StartsWith(allowedNamespace, StringComparison.OrdinalIgnoreCase)) { From a90b1b933dd3b4c31372d49023ec0f3c183d3443 Mon Sep 17 00:00:00 2001 From: Normen Scheiber Date: Fri, 19 Jan 2024 11:47:54 +0100 Subject: [PATCH 4/4] test fix. --- src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs b/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs index df96cd5..3835e29 100644 --- a/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs +++ b/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs @@ -199,7 +199,7 @@ public void GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOper .IncludeField("**.planSummary") .ExcludeField("**.$db") .ExcludeField("**.lsid") - .ExcludeField("execStats") + .ExcludeField("**.execStats") ); }