From 0ea1232380e803f9480800605edc21f18c14e5c6 Mon Sep 17 00:00:00 2001 From: Jake Soenneker Date: Wed, 20 Nov 2024 14:59:03 -0600 Subject: [PATCH] Protects against top level type generation of delegates --- src/Generators/Types/TypeGenerator.cs | 8 +++++ .../AutoFakerTests.cs | 35 +++++++++++++++++++ .../Simple/TestClassWithAutoPropertyAction.cs | 9 +++++ .../Simple/TestClassWithAutoPropertyFunc.cs | 9 +++++ 4 files changed, 61 insertions(+) create mode 100644 test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithAutoPropertyAction.cs create mode 100644 test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithAutoPropertyFunc.cs diff --git a/src/Generators/Types/TypeGenerator.cs b/src/Generators/Types/TypeGenerator.cs index e375476..8a43604 100644 --- a/src/Generators/Types/TypeGenerator.cs +++ b/src/Generators/Types/TypeGenerator.cs @@ -4,6 +4,10 @@ namespace Soenneker.Utils.AutoBogus.Generators.Types; +/// +/// This is the start of the journey of any top level type, and is used recursively +/// +/// internal sealed class TypeGenerator : IAutoFakerGenerator { object? IAutoFakerGenerator.Generate(AutoFakerContext context) @@ -13,6 +17,10 @@ internal sealed class TypeGenerator : 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, Generate>, etc + if (cachedType.IsDelegate) + return null; + object? instance = context.Binder.CreateInstanceWithRecursionGuard(context, cachedType); if (instance == null) diff --git a/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs index 6367c7a..c96f6f6 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs +++ b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs @@ -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; @@ -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(); + result.Should().BeNull(); + } + + [Fact] + public void Generate_Func_should_be_null() + { + AutoFaker generator = new(); + var result = generator.Generate>(); + result.Should().BeNull(); + } + + [Fact] + public void Generate_TestClassWithAutoPropertyAction_should_not_be_null_but_property_should_be() + { + AutoFaker generator = new(); + var result = generator.Generate(); + 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(); + result.Should().NotBeNull(); + result.Func.Should().BeNull(); + } + [Fact] public void Generate_TestClassWithReadOnlyStringField_should_be_null() { diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithAutoPropertyAction.cs b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithAutoPropertyAction.cs new file mode 100644 index 0000000..a368977 --- /dev/null +++ b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithAutoPropertyAction.cs @@ -0,0 +1,9 @@ +using System; + +namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple; + +internal class TestClassWithAutoPropertyAction +{ + public Action Action { get; set; } + +} \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithAutoPropertyFunc.cs b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithAutoPropertyFunc.cs new file mode 100644 index 0000000..72b1ef7 --- /dev/null +++ b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithAutoPropertyFunc.cs @@ -0,0 +1,9 @@ +using System; + +namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple; + +internal class TestClassWithAutoPropertyFunc +{ + public Func Func { get; set; } + +} \ No newline at end of file