Skip to content

Commit a726f09

Browse files
committed
we cannot reverse open generics with ForPath because ForPath doesn't work with strings
1 parent 805e21f commit a726f09

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/AutoMapper/Configuration/TypeMapConfiguration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ private void AddValueTransformers(TypeMap typeMap)
104104
}
105105
private void ConfigureReverseMap(TypeMap typeMap)
106106
{
107-
ReverseSourceMembers(typeMap);
107+
if (!typeMap.Types.ContainsGenericParameters)
108+
{
109+
ReverseSourceMembers(typeMap);
110+
}
108111
foreach (var destProperty in typeMap.PropertyMaps.Where(pm => pm.Ignored))
109112
{
110113
ReverseMapExpression.ForSourceMemberCore(destProperty.DestinationName, opt => opt.DoNotValidate());

src/UnitTests/OpenGenerics.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
namespace AutoMapper.UnitTests;
2+
public class ForPathGenericsSource : AutoMapperSpecBase
3+
{
4+
class Source<T>
5+
{
6+
public InnerSource Inner;
7+
}
8+
class InnerSource
9+
{
10+
public int Id;
11+
}
12+
class Destination
13+
{
14+
public int InnerId;
15+
}
16+
protected override MapperConfiguration CreateConfiguration() => new(c => c.CreateMap(typeof(Source<>), typeof(Destination)).ReverseMap());
17+
[Fact]
18+
public void Should_work() => Map<Destination>(new Source<int> { Inner = new() { Id = 42 } }).InnerId.ShouldBe(42);
19+
}
20+
public class ForPathGenerics : AutoMapperSpecBase
21+
{
22+
class Source<T>
23+
{
24+
public InnerSource Inner;
25+
}
26+
class InnerSource
27+
{
28+
public int Id;
29+
}
30+
class Destination<T>
31+
{
32+
public int InnerId;
33+
}
34+
protected override MapperConfiguration CreateConfiguration() => new(c => c.CreateMap(typeof(Source<>), typeof(Destination<>)).ReverseMap());
35+
[Fact]
36+
public void Should_work() => Map<Destination<int>>(new Source<int> { Inner = new() { Id = 42 } }).InnerId.ShouldBe(42);
37+
}
238
public class ReadonlyPropertiesGenerics : AutoMapperSpecBase
339
{
440
class Source

0 commit comments

Comments
 (0)