Skip to content

Commit

Permalink
Add posibility to exclude namespaces (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
glucaci committed Jan 26, 2024
1 parent d64ffbc commit c83bf4e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/Context.Tests/Internal/DependencyTypesResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public void GetAllowedTypesByDependencies_All_Successful()

// Act
IEnumerable<string> knownNamespaces = DependencyTypesResolver
.GetAllowedTypesByDependencies()
.Except(new[] { "Coverlet" })
.GetAllowedTypesByDependencies(new[] { "Coverlet" })
.OrderBy(x => x);

// Assert
Expand Down
2 changes: 1 addition & 1 deletion src/Context/IMongoDatabaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public interface IMongoDatabaseBuilder

IMongoDatabaseBuilder AddAllowedTypes(params string[] allowedNamespaces);

IMongoDatabaseBuilder AddAllowedTypesOfAllDependencies();
IMongoDatabaseBuilder AddAllowedTypesOfAllDependencies(params string[] excludeNamespaces);
}
}
12 changes: 6 additions & 6 deletions src/Context/Internal/DependencyTypesResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@

namespace MongoDB.Extensions.Context;

internal class DependencyTypesResolver
internal static class DependencyTypesResolver
{
private static readonly HashSet<string> _notAllowedNames =
new HashSet<string>{ "System", "Microsoft" };

public static HashSet<string> GetAllowedTypesByDependencies()
internal static HashSet<string> GetAllowedTypesByDependencies(string[] excludeNamespaces)
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

IEnumerable<Assembly> allowedAssemblies = assemblies
.Where(assembly => IsNamespaceAllowed(assembly.GetName().Name));
.Where(assembly => IsNamespaceAllowed(assembly.GetName().Name, excludeNamespaces));

Check warning on line 19 in src/Context/Internal/DependencyTypesResolver.cs

View workflow job for this annotation

GitHub Actions / sonar

Possible null reference argument for parameter 'name' in 'bool DependencyTypesResolver.IsNamespaceAllowed(string name, string[] excludeNamespaces)'.

Check warning on line 19 in src/Context/Internal/DependencyTypesResolver.cs

View workflow job for this annotation

GitHub Actions / release

Possible null reference argument for parameter 'name' in 'bool DependencyTypesResolver.IsNamespaceAllowed(string name, string[] excludeNamespaces)'.

Check warning on line 19 in src/Context/Internal/DependencyTypesResolver.cs

View workflow job for this annotation

GitHub Actions / release

Possible null reference argument for parameter 'name' in 'bool DependencyTypesResolver.IsNamespaceAllowed(string name, string[] excludeNamespaces)'.

IEnumerable<string> namespaces = allowedAssemblies
.SelectMany(a => a.GetTypes())
.Select(type => type.GetRootNamespace())
.Where(name => IsNamespaceAllowed(name));
.Where(name => IsNamespaceAllowed(name, excludeNamespaces));

return new HashSet<string>(namespaces);
}

private static bool IsNamespaceAllowed(string name)
private static bool IsNamespaceAllowed(string name, string [] excludeNamespaces)
{
if(string.IsNullOrEmpty(name))
{
return false;
}

return !_notAllowedNames
return !_notAllowedNames.Concat(excludeNamespaces)
.Any(entry => name.StartsWith(entry));
}
}
4 changes: 2 additions & 2 deletions src/Context/Internal/MongoDatabaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public IMongoDatabaseBuilder AddAllowedTypes(params string[] allowedNamespaces)
return this;
}

public IMongoDatabaseBuilder AddAllowedTypesOfAllDependencies()
public IMongoDatabaseBuilder AddAllowedTypesOfAllDependencies(params string[] excludeNamespaces)
{
TypeObjectSerializer.AddAllowedTypesOfAllDependencies();
TypeObjectSerializer.AddAllowedTypesOfAllDependencies(excludeNamespaces);
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Context/TypeObjectSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public static void AddAllowedTypes(params string[] allowedNamespaces)
}
}

public static void AddAllowedTypesOfAllDependencies()
public static void AddAllowedTypesOfAllDependencies(params string[] excludeNamespaces)
{
_allowedTypesByDependencies
.UnionWith(DependencyTypesResolver.GetAllowedTypesByDependencies());
.UnionWith(DependencyTypesResolver.GetAllowedTypesByDependencies(excludeNamespaces));
}

internal static void Clear()
Expand Down

0 comments on commit c83bf4e

Please sign in to comment.