Skip to content

Commit

Permalink
Protects against top level type generation of delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
soenneker committed Nov 20, 2024
1 parent 1cdca69 commit 0ea1232
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Generators/Types/TypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Soenneker.Utils.AutoBogus.Generators.Types;

/// <summary>
/// This is the start of the journey of any top level type, and is used recursively
/// </summary>
/// <typeparam name="TType"></typeparam>
internal sealed class TypeGenerator<TType> : IAutoFakerGenerator
{
object? IAutoFakerGenerator.Generate(AutoFakerContext context)
Expand All @@ -13,6 +17,10 @@ internal sealed class TypeGenerator<TType> : IAutoFakerGenerator
// This means the changes are applied to a different instance to the one created here
CachedType cachedType = context.CacheService.Cache.GetCachedType(typeof(TType));

// Protects against Generate<Action>, Generate<Func<>>, etc
if (cachedType.IsDelegate)
return null;

object? instance = context.Binder.CreateInstanceWithRecursionGuard<TType>(context, cachedType);

if (instance == null)
Expand Down
35 changes: 35 additions & 0 deletions test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Soenneker.Utils.AutoBogus.Config;
using Soenneker.Utils.AutoBogus.Tests.Dtos;
using Soenneker.Utils.AutoBogus.Tests.Dtos.Simple.Delegates;
using System;

namespace Soenneker.Utils.AutoBogus.Tests;

Expand Down Expand Up @@ -378,6 +379,40 @@ public void TestClassWithFuncCtor_should_be_null()
obj.Should().BeNull();
}

[Fact]
public void Generate_Action_should_be_null()
{
AutoFaker generator = new();
var result = generator.Generate<Action>();
result.Should().BeNull();
}

[Fact]
public void Generate_Func_should_be_null()
{
AutoFaker generator = new();
var result = generator.Generate<Func<string, string>>();
result.Should().BeNull();
}

[Fact]
public void Generate_TestClassWithAutoPropertyAction_should_not_be_null_but_property_should_be()
{
AutoFaker generator = new();
var result = generator.Generate<TestClassWithAutoPropertyAction>();
result.Should().NotBeNull();
result.Action.Should().BeNull();
}

[Fact]
public void Generate_TestClassWithAutoPropertyFunc_should_not_be_null_but_property_should_be()
{
AutoFaker generator = new();
var result = generator.Generate<TestClassWithAutoPropertyFunc>();
result.Should().NotBeNull();
result.Func.Should().BeNull();
}

[Fact]
public void Generate_TestClassWithReadOnlyStringField_should_be_null()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple;

internal class TestClassWithAutoPropertyAction
{
public Action Action { get; set; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple;

internal class TestClassWithAutoPropertyFunc
{
public Func<string, string> Func { get; set; }

}

0 comments on commit 0ea1232

Please sign in to comment.