Skip to content

Commit 1aea726

Browse files
authored
Merge pull request AutoMapper#2136 from AutoMapper/DuplicateCreateMapCalls
Disallow duplicate create map calls
2 parents 55e54ca + c047a78 commit 1aea726

11 files changed

+1678
-1508
lines changed

src/AutoMapper/AdvancedConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class AdvancedConfiguration
2222
/// <param name="validator">the validation callback</param>
2323
public void Validator(Validator validator) => _validators.Add(validator);
2424

25+
public bool AllowAdditiveTypeMapCreation { get; set; }
26+
2527
internal Validator[] GetValidators() => _validators.ToArray();
2628
}
2729
}

src/AutoMapper/Configuration/MappingExpression.cs

Lines changed: 520 additions & 520 deletions
Large diffs are not rendered by default.

src/AutoMapper/ConfigurationValidator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using AutoMapper.Configuration;
5-
using AutoMapper.Mappers;
65

76
namespace AutoMapper
87
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace AutoMapper
5+
{
6+
public class DuplicateTypeMapConfigurationException : Exception
7+
{
8+
public TypeMapConfigErrors[] Errors { get; }
9+
10+
public DuplicateTypeMapConfigurationException(TypeMapConfigErrors[] errors)
11+
{
12+
Errors = errors;
13+
var builder = new StringBuilder();
14+
builder.AppendLine("The following type maps were found in multiple profiles:");
15+
foreach (var error in Errors)
16+
{
17+
builder.AppendLine($"{error.Types.SourceType.FullName} to {error.Types.DestinationType.FullName} defined in profiles:");
18+
builder.AppendLine(string.Join(Environment.NewLine, error.ProfileNames));
19+
}
20+
builder.AppendLine("This can cause configuration collisions and inconsistent mapping.");
21+
builder.AppendLine("Consolidate the CreateMap calls into one profile, or set the root Advanced.AllowAdditiveTypeMapCreation configuration value to 'true'.");
22+
23+
Message = builder.ToString();
24+
}
25+
26+
public class TypeMapConfigErrors
27+
{
28+
public string[] ProfileNames { get; }
29+
public TypePair Types { get; }
30+
31+
public TypeMapConfigErrors(TypePair types, string[] profileNames)
32+
{
33+
Types = types;
34+
ProfileNames = profileNames;
35+
}
36+
}
37+
38+
public override string Message { get; }
39+
}
40+
}

0 commit comments

Comments
 (0)