Skip to content

Commit

Permalink
fix: obsolete constructors should have the last priority whlie matchi…
Browse files Browse the repository at this point in the history
…ng constructors (regression)
  • Loading branch information
latonz committed Nov 14, 2023
1 parent 6cceb6d commit 2c2f77f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private static void BuildConstructorMapping(INewInstanceBuilderContext<IMapping>
var ctorCandidates = namedTargetType.InstanceConstructors
.Where(ctor => ctx.BuilderContext.SymbolAccessor.IsDirectlyAccessible(ctor))
.OrderByDescending(x => ctx.BuilderContext.SymbolAccessor.HasAttribute<MapperConstructorAttribute>(x))
.ThenBy(x => ctx.BuilderContext.SymbolAccessor.HasAttribute<MapperConstructorAttribute>(x))
.ThenBy(x => ctx.BuilderContext.SymbolAccessor.HasAttribute<ObsoleteAttribute>(x))
.ThenByDescending(x => x.Parameters.Length == 0)
.ThenByDescending(x => x.Parameters.Length);
foreach (var ctorCandidate in ctorCandidates)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,16 @@ 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
.GenerateMapper(source)
.Should()
.HaveSingleMethodBody(
"""
var target = new global::B(source.StringValue, source.IntValue);
var target = new global::B(source.StringValue);
target.IntValue = source.IntValue;
return target;
"""
);
Expand Down

0 comments on commit 2c2f77f

Please sign in to comment.