From 2b31175e438db2ae2ae9972205f6b26ac2d3fe7a Mon Sep 17 00:00:00 2001 From: Arnaud Boussaer Date: Thu, 3 Nov 2022 13:49:31 +0100 Subject: [PATCH] Also skip members defined via interface --- .../Complex/IWithCode.cs | 9 ++++++++ src/AutoBogus.Tests.Models/Complex/Order.cs | 4 ++-- src/AutoBogus.Tests/AutoFakerFixture.cs | 12 ++++++++++ src/AutoBogus/AutoBinder.cs | 23 ++++++++++++------- 4 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 src/AutoBogus.Tests.Models/Complex/IWithCode.cs diff --git a/src/AutoBogus.Tests.Models/Complex/IWithCode.cs b/src/AutoBogus.Tests.Models/Complex/IWithCode.cs new file mode 100644 index 0000000..9d9a95a --- /dev/null +++ b/src/AutoBogus.Tests.Models/Complex/IWithCode.cs @@ -0,0 +1,9 @@ +using System; + +namespace AutoBogus.Tests.Models.Complex +{ + public interface IWithCode + { + Guid? Code { get; set; } + } +} diff --git a/src/AutoBogus.Tests.Models/Complex/Order.cs b/src/AutoBogus.Tests.Models/Complex/Order.cs index 0d86db5..dd5e6ff 100644 --- a/src/AutoBogus.Tests.Models/Complex/Order.cs +++ b/src/AutoBogus.Tests.Models/Complex/Order.cs @@ -1,9 +1,9 @@ -using System; +using System; using System.Collections.Generic; namespace AutoBogus.Tests.Models.Complex { - public sealed class Order + public sealed class Order : IWithCode { public DateTime Timestamp; diff --git a/src/AutoBogus.Tests/AutoFakerFixture.cs b/src/AutoBogus.Tests/AutoFakerFixture.cs index 88627da..e2d82e9 100644 --- a/src/AutoBogus.Tests/AutoFakerFixture.cs +++ b/src/AutoBogus.Tests/AutoFakerFixture.cs @@ -395,6 +395,18 @@ public void Should_Skip_Configured_Members() instance.Discounts.Should().BeNull(); instance.Items.Should().OnlyContain(i => i.Discounts == null); } + + [Fact] + public void Should_Skip_Configured_Members_Of_Interface() + { + var instance = AutoFaker.Generate(builder => + { + builder + .WithSkip(i => i.Code); + }); + + instance.Code.Should().BeNull(); + } } public class Behaviors_Types diff --git a/src/AutoBogus/AutoBinder.cs b/src/AutoBogus/AutoBinder.cs index e36df7b..2f5f373 100644 --- a/src/AutoBogus/AutoBinder.cs +++ b/src/AutoBogus/AutoBinder.cs @@ -72,7 +72,7 @@ public virtual void PopulateInstance(object instance, AutoGenerateContext { // Check if the member has a skip config or the type has already been generated as a parent // If so skip this generation otherwise track it for use later in the object tree - if (ShouldSkip(member.Type, $"{type.FullName}.{member.Name}", context)) + if (ShouldSkip(type, member, context)) { continue; } @@ -111,19 +111,26 @@ public virtual void PopulateInstance(object instance, AutoGenerateContext } } - private bool ShouldSkip(Type type, string path, AutoGenerateContext context) + private bool ShouldSkip(Type type, AutoMember member, AutoGenerateContext context) { - // Skip if the type is found - if (context.Config.SkipTypes.Contains(type)) + // Skip if the member type is found + if (context.Config.SkipTypes.Contains(member.Type)) { return true; } - // Skip if the path is found - if (context.Config.SkipPaths.Contains(path)) + // Skip if the path is found (both current type and its implemented interfaces) + if (context.Config.SkipPaths.Contains($"{type.FullName}.{member.Name}")) { return true; } + foreach (var implementedInterfaceType in type.GetInterfaces()) + { + if (context.Config.SkipPaths.Contains($"{implementedInterfaceType.FullName}.{member.Name}")) + { + return true; + } + } //check if tree depth is reached var treeDepth = context.Config.TreeDepth.Invoke(context); @@ -132,8 +139,8 @@ private bool ShouldSkip(Type type, string path, AutoGenerateContext context) return true; // Finally check if the recursive depth has been reached - - var count = context.TypesStack.Count(t => t == type); + + var count = context.TypesStack.Count(t => t == member.Type); var recursiveDepth = context.Config.RecursiveDepth.Invoke(context); return count >= recursiveDepth;