From c7ccc75a3b6f0517edfb2d55e7a02be8c0cb7fee Mon Sep 17 00:00:00 2001 From: Timothy Makkison Date: Thu, 28 Mar 2024 21:05:38 +0000 Subject: [PATCH] chore: semi fix --- .../MapperConfigurationReader.cs | 10 +--- .../InlineExpressionMappingBuilderContext.cs | 25 ++++++--- .../Helpers/MapperConfigurationBuilderTest.cs | 3 + .../Mapping/QueryableProjectionLoopTest.cs | 1 - .../Mapping/QueryableProjectionTest.cs | 55 +++++-------------- 5 files changed, 38 insertions(+), 56 deletions(-) diff --git a/src/Riok.Mapperly/Configuration/MapperConfigurationReader.cs b/src/Riok.Mapperly/Configuration/MapperConfigurationReader.cs index c1e5e111b2..c4a80aa446 100644 --- a/src/Riok.Mapperly/Configuration/MapperConfigurationReader.cs +++ b/src/Riok.Mapperly/Configuration/MapperConfigurationReader.cs @@ -87,12 +87,7 @@ private MembersMappingConfiguration BuildMembersConfig(MappingConfigurationRefer .AccessFirstOrDefault(configRef.Method) ?.IgnoreObsoleteStrategy; var requiredMapping = _dataAccessor.AccessFirstOrDefault(configRef.Method)?.RequiredMappingStrategy; - var maxRecursionDepth = _dataAccessor.Access(method).FirstOrDefault() - is not { } methodMaxRecursionDepth - ? _defaultConfiguration.Properties.MaxRecursionDepth - : methodMaxRecursionDepth.MaxRecursionDepth; - - + var maxRecursionDepth = _dataAccessor.AccessFirstOrDefault(configRef.Method)?.MaxRecursionDepth; // ignore the required mapping / ignore obsolete as the same attribute is used for other mapping types // e.g. enum to enum @@ -123,7 +118,8 @@ is not { } methodMaxRecursionDepth ignoredTargetMembers, memberConfigurations, ignoreObsolete ?? MapperConfiguration.Members.IgnoreObsoleteMembersStrategy, - requiredMapping ?? MapperConfiguration.Members.RequiredMappingStrategy + requiredMapping ?? MapperConfiguration.Members.RequiredMappingStrategy, + maxRecursionDepth ?? MapperConfiguration.Members.MaxRecursionDepth ); } diff --git a/src/Riok.Mapperly/Descriptors/InlineExpressionMappingBuilderContext.cs b/src/Riok.Mapperly/Descriptors/InlineExpressionMappingBuilderContext.cs index cc43e389c0..73edad4327 100644 --- a/src/Riok.Mapperly/Descriptors/InlineExpressionMappingBuilderContext.cs +++ b/src/Riok.Mapperly/Descriptors/InlineExpressionMappingBuilderContext.cs @@ -77,6 +77,15 @@ conversionType is not MappingConversionType.EnumToString and not MappingConversi /// The if a mapping was found or null if none was found. public override INewInstanceMapping? FindMapping(TypeMappingKey mappingKey) { + // check for recursion loop returning null to prevent a loop or default when recursion limit is reached. + var count = _parentTypes.GetDepth(mappingKey); + if (count >= 1) + { + return count >= Configuration.Members.MaxRecursionDepth + 2 + ? new DefaultMemberMapping(mappingKey.Source, mappingKey.Target) + : null; + } + var mapping = InlinedMappings.Find(mappingKey, out var isInlined); if (mapping == null) return null; @@ -91,14 +100,14 @@ conversionType is not MappingConversionType.EnumToString and not MappingConversi public INewInstanceMapping? FindNewInstanceMapping(IMethodSymbol method) { - // check for recursion loop returning null to prevent a loop or default when recursion limit is reached. - var count = _parentTypes.GetDepth(mappingKey); - if (count >= 1) - { - return count >= Configuration.Properties.MaxRecursionDepth + 2 - ? new DefaultMemberMapping(mappingKey.Source, mappingKey.Target) - : null; - } + // // check for recursion loop returning null to prevent a loop or default when recursion limit is reached. + // var count = _parentTypes.GetDepth(mappingKey); + // if (count >= 1) + // { + // return count >= Configuration.Properties.MaxRecursionDepth + 2 + // ? new DefaultMemberMapping(mappingKey.Source, mappingKey.Target) + // : null; + // } INewInstanceMapping? mapping = InlinedMappings.FindNewInstanceUserMapping(method, out var isInlined); if (mapping == null) diff --git a/test/Riok.Mapperly.Tests/Helpers/MapperConfigurationBuilderTest.cs b/test/Riok.Mapperly.Tests/Helpers/MapperConfigurationBuilderTest.cs index a9fc4168de..6533975e28 100644 --- a/test/Riok.Mapperly.Tests/Helpers/MapperConfigurationBuilderTest.cs +++ b/test/Riok.Mapperly.Tests/Helpers/MapperConfigurationBuilderTest.cs @@ -94,6 +94,9 @@ private MapperConfiguration NewMapperConfiguration() if (type == typeof(bool)) return !modifiedValue; + if (type == typeof(int)) + return !modifiedValue; + if (type.IsEnum) return type.GetEnumValues().GetValue(modifiedValue ? 1 : 0); diff --git a/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionLoopTest.cs b/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionLoopTest.cs index 17c368f97e..36a22a79d5 100644 --- a/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionLoopTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionLoopTest.cs @@ -2,7 +2,6 @@ namespace Riok.Mapperly.Tests.Mapping; -[UsesVerify] public class QueryableProjectionLoopTest { [Fact] diff --git a/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionTest.cs b/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionTest.cs index f8fa59ad0b..e833374829 100644 --- a/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionTest.cs @@ -116,46 +116,21 @@ public Task RecordToRecordManualListMapping() return TestHelper.VerifyGenerator(source); } - [Fact] - public Task ClassToClassWithUserImplemented() - { - var source = TestSourceBuilder.MapperWithBodyAndTypes( - """ - private partial System.Linq.IQueryable Map(System.Linq.IQueryable source); - - private D MapToD(C v) => new D { Value = v.Value + "-mapped" }; - """, - "class A { public string StringValue { get; set; } public C NestedValue { get; set; } }", - "class B { public string StringValue { get; set; } public D NestedValue { get; set; } }", - "class C { public string Value { get; set; } }", - "class D { public string Value { get; set; } }" - ); - - [Fact] - public Task ReferenceLoopInitProperty() - { - var source = TestSourceBuilder.Mapping( - "System.Linq.IQueryable", - "System.Linq.IQueryable", - "class A { public A? Parent { get; set; } public int IntValue { get; set; } }", - "class B { public B? Parent { get; set; } public int IntValue { get; set; } }" - ); - - return TestHelper.VerifyGenerator(source); - } - - [Fact] - public Task ReferenceLoopCtor() - { - var source = TestSourceBuilder.Mapping( - "System.Linq.IQueryable", - "System.Linq.IQueryable", - "class A { public A? Parent { get; set; } public int IntValue { get; set; } }", - "class B { public B(B? parent) {} public int IntValue { get; set; } }" - ); - - return TestHelper.VerifyGenerator(source); - } + // [Fact] + // public Task ClassToClassWithUserImplemented() + // { + // var source = TestSourceBuilder.MapperWithBodyAndTypes( + // """ + // private partial System.Linq.IQueryable Map(System.Linq.IQueryable source); + // + // private D MapToD(C v) => new D { Value = v.Value + "-mapped" }; + // """, + // "class A { public string StringValue { get; set; } public C NestedValue { get; set; } }", + // "class B { public string StringValue { get; set; } public D NestedValue { get; set; } }", + // "class C { public string Value { get; set; } }", + // "class D { public string Value { get; set; } }" + // ); + // } [Fact] public Task CtorShouldSkipUnmatchedOptionalParameters()