Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix add allowed types #90

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading