Skip to content

Commit

Permalink
Merge branch 'master' into sessionProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
nscheibe committed Jan 19, 2024
2 parents 3ba7ca0 + 2366108 commit 5a2a208
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<ProjectReference Include="..\Context\Context.csproj" />
<ProjectReference Include="..\Prime.Extensions\Prime.Extensions.csproj" />
</ItemGroup>

</Project>
32 changes: 31 additions & 1 deletion src/Context.AllowedTypes.Tests/TypeObjectSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void IsTypeAllowed_InAllowedNamespaces_True()
}

[Fact]
public void IsTypeAllowed_InAllowedNamespaces_False()
public void IsTypeAllowed_PartIsInAllowedNamespaces_True()
{
// Arrange
TypeObjectSerializer.Clear();
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"AllowedTypes": [
{
"Key": "MongoDB.Extensions.Context.AllowedTypes.Tests.Foo",
"Value": true
}
],
"AllowedTypesByNamespaces": [
"MONGODB.EXTENSIONS"
],
"AllowedTypesByDependencies": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"AllowedTypes": [
{
"Key": "MongoDB.Extensions.Context.AllowedTypes.Tests.Foo",
"Value": false
"Value": true
}
],
"AllowedTypesByNamespaces": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"AllowedTypes": [
{
"Key": "MongoDB.Extensions.Context.AllowedTypes.Tests.Foo",
"Value": false
}
],
"AllowedTypesByNamespaces": [
"MongoDBs.Context"
],
"AllowedTypesByDependencies": []
}
28 changes: 23 additions & 5 deletions src/Context/TypeObjectSerializer.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -70,22 +70,40 @@ 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);
}

return isInAllowedNamespaces;
}

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))
{
return true;
}
}

return false;
}

private static bool IsInAllowedDependencyTypes(Type type)
{
bool isInDependencyTypes = _allowedTypesByDependencies
Expand Down
2 changes: 1 addition & 1 deletion src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOper
.IncludeField("**.planSummary")
.ExcludeField("**.$db")
.ExcludeField("**.lsid")
.ExcludeField("execStats")
.ExcludeField("**.execStats")
);
}

Expand Down

0 comments on commit 5a2a208

Please sign in to comment.