From 2c2f77f9493eb87949c258a3119c1d9aabf7464f Mon Sep 17 00:00:00 2001 From: latonz Date: Tue, 14 Nov 2023 18:19:47 +0530 Subject: [PATCH] fix: obsolete constructors should have the last priority whlie matching constructors (regression) --- .../NewInstanceObjectMemberMappingBodyBuilder.cs | 2 +- .../Mapping/ObjectPropertyConstructorResolverTest.cs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/NewInstanceObjectMemberMappingBodyBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/NewInstanceObjectMemberMappingBodyBuilder.cs index 60b53d971d..6ba519793d 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/NewInstanceObjectMemberMappingBodyBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/NewInstanceObjectMemberMappingBodyBuilder.cs @@ -208,7 +208,7 @@ private static void BuildConstructorMapping(INewInstanceBuilderContext var ctorCandidates = namedTargetType.InstanceConstructors .Where(ctor => ctx.BuilderContext.SymbolAccessor.IsDirectlyAccessible(ctor)) .OrderByDescending(x => ctx.BuilderContext.SymbolAccessor.HasAttribute(x)) - .ThenBy(x => ctx.BuilderContext.SymbolAccessor.HasAttribute(x)) + .ThenBy(x => ctx.BuilderContext.SymbolAccessor.HasAttribute(x)) .ThenByDescending(x => x.Parameters.Length == 0) .ThenByDescending(x => x.Parameters.Length); foreach (var ctorCandidate in ctorCandidates) diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs index d79e892eec..0490dc9fca 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs @@ -215,7 +215,7 @@ public void ClassToClassMultipleCtorsShouldPreferNonObsolete() "A", "B", "class A { public string StringValue { get; set; } public int IntValue { get; set; } }", - "class B { [Obsolete] public B(string StringValue) { } public B(string stringValue, int intvalue) { } { public string StringValue { get; set; } public int IntValue { get; set; } " + "class B { [Obsolete] public B(string stringValue, int intValue) { } public B(string stringValue) { } { public string StringValue { get; set; } public int IntValue { get; set; } }" ); TestHelper @@ -223,7 +223,8 @@ public void ClassToClassMultipleCtorsShouldPreferNonObsolete() .Should() .HaveSingleMethodBody( """ - var target = new global::B(source.StringValue, source.IntValue); + var target = new global::B(source.StringValue); + target.IntValue = source.IntValue; return target; """ );