|
1 | 1 | using AutoMapper.Internal.Mappers; |
2 | 2 | namespace AutoMapper.Configuration; |
3 | 3 | [EditorBrowsable(EditorBrowsableState.Never)] |
4 | | -public readonly record struct ConfigurationValidator(IGlobalConfigurationExpression Expression) |
| 4 | +public class ConfigurationValidator(IGlobalConfiguration config) |
5 | 5 | { |
6 | | - private void Validate(ValidationContext context) |
7 | | - { |
8 | | - foreach (var validator in Expression.Validators) |
9 | | - { |
10 | | - validator(context); |
11 | | - } |
12 | | - } |
13 | | - public void AssertConfigurationExpressionIsValid(IGlobalConfiguration config, TypeMap[] typeMaps) |
| 6 | + IGlobalConfigurationExpression Expression => ((MapperConfiguration)config).ConfigurationExpression; |
| 7 | + public void AssertConfigurationExpressionIsValid(TypeMap[] typeMaps) |
14 | 8 | { |
15 | 9 | var duplicateTypeMapConfigs = Expression.Profiles.Append((Profile)Expression) |
16 | 10 | .SelectMany(p => p.TypeMapConfigs, (profile, typeMap) => (profile, typeMap)) |
17 | 11 | .GroupBy(x => x.typeMap.Types) |
18 | 12 | .Where(g => g.Count() > 1) |
19 | | - .Select(g => (TypePair : g.Key, ProfileNames : g.Select(tmc => tmc.profile.ProfileName).ToArray())) |
| 13 | + .Select(g => (TypePair: g.Key, ProfileNames: g.Select(tmc => tmc.profile.ProfileName).ToArray())) |
20 | 14 | .Select(g => new DuplicateTypeMapConfigurationException.TypeMapConfigErrors(g.TypePair, g.ProfileNames)) |
21 | 15 | .ToArray(); |
22 | | - if (duplicateTypeMapConfigs.Any()) |
| 16 | + if(duplicateTypeMapConfigs.Length != 0) |
23 | 17 | { |
24 | 18 | throw new DuplicateTypeMapConfigurationException(duplicateTypeMapConfigs); |
25 | 19 | } |
26 | | - AssertConfigurationIsValid(config, typeMaps); |
| 20 | + AssertConfigurationIsValid(typeMaps); |
27 | 21 | } |
28 | | - public void AssertConfigurationIsValid(IGlobalConfiguration config, TypeMap[] typeMaps) |
| 22 | + public void AssertConfigurationIsValid(TypeMap[] typeMaps) |
29 | 23 | { |
| 24 | + List<Exception> configExceptions = []; |
30 | 25 | var badTypeMaps = |
31 | 26 | (from typeMap in typeMaps |
32 | | - where typeMap.ShouldCheckForValid |
33 | | - let unmappedPropertyNames = typeMap.GetUnmappedPropertyNames() |
34 | | - let canConstruct = typeMap.PassesCtorValidation |
35 | | - where unmappedPropertyNames.Length > 0 || !canConstruct |
36 | | - select new AutoMapperConfigurationException.TypeMapConfigErrors(typeMap, unmappedPropertyNames, canConstruct) |
37 | | - ).ToArray(); |
38 | | - if (badTypeMaps.Length > 0) |
| 27 | + where typeMap.ShouldCheckForValid |
| 28 | + let unmappedPropertyNames = typeMap.GetUnmappedPropertyNames() |
| 29 | + let canConstruct = typeMap.PassesCtorValidation |
| 30 | + where unmappedPropertyNames.Length > 0 || !canConstruct |
| 31 | + select new AutoMapperConfigurationException.TypeMapConfigErrors(typeMap, unmappedPropertyNames, canConstruct)).ToArray(); |
| 32 | + if(badTypeMaps.Length > 0) |
39 | 33 | { |
40 | | - throw new AutoMapperConfigurationException(badTypeMaps); |
| 34 | + configExceptions.Add(new AutoMapperConfigurationException(badTypeMaps)); |
41 | 35 | } |
42 | 36 | HashSet<TypeMap> typeMapsChecked = []; |
43 | | - List<Exception> configExceptions = []; |
44 | | - foreach (var typeMap in typeMaps) |
| 37 | + foreach(var typeMap in typeMaps) |
45 | 38 | { |
46 | | - try |
47 | | - { |
48 | | - DryRunTypeMap(config, typeMapsChecked, typeMap.Types, typeMap, null); |
49 | | - } |
50 | | - catch (Exception e) |
51 | | - { |
52 | | - configExceptions.Add(e); |
53 | | - } |
| 39 | + DryRunTypeMap(typeMap.Types, typeMap, null); |
54 | 40 | } |
55 | | - if (configExceptions.Count > 1) |
| 41 | + if(configExceptions.Count > 1) |
56 | 42 | { |
57 | 43 | throw new AggregateException(configExceptions); |
58 | 44 | } |
59 | | - if (configExceptions.Count > 0) |
| 45 | + if(configExceptions.Count > 0) |
60 | 46 | { |
61 | 47 | throw configExceptions[0]; |
62 | 48 | } |
63 | | - } |
64 | | - private void DryRunTypeMap(IGlobalConfiguration config, HashSet<TypeMap> typeMapsChecked, TypePair types, TypeMap typeMap, MemberMap memberMap) |
65 | | - { |
66 | | - if(typeMap == null) |
| 49 | + void DryRunTypeMap(TypePair types, TypeMap typeMap, MemberMap memberMap) |
67 | 50 | { |
68 | | - if (types.ContainsGenericParameters) |
| 51 | + if(typeMap == null) |
69 | 52 | { |
70 | | - return; |
| 53 | + if(types.ContainsGenericParameters) |
| 54 | + { |
| 55 | + return; |
| 56 | + } |
| 57 | + typeMap = config.ResolveTypeMap(types.SourceType, types.DestinationType); |
71 | 58 | } |
72 | | - typeMap = config.ResolveTypeMap(types.SourceType, types.DestinationType); |
73 | | - } |
74 | | - if (typeMap != null) |
75 | | - { |
76 | | - if (typeMapsChecked.Contains(typeMap)) |
| 59 | + if(typeMap != null) |
77 | 60 | { |
78 | | - return; |
| 61 | + if(typeMapsChecked.Contains(typeMap)) |
| 62 | + { |
| 63 | + return; |
| 64 | + } |
| 65 | + typeMapsChecked.Add(typeMap); |
| 66 | + if(!Validate(new(types, memberMap, typeMap)) || !typeMap.ShouldCheckForValid) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + CheckPropertyMaps(typeMap); |
79 | 71 | } |
80 | | - typeMapsChecked.Add(typeMap); |
81 | | - Validate(new(types, memberMap, typeMap)); |
82 | | - if(!typeMap.ShouldCheckForValid) |
| 72 | + else |
83 | 73 | { |
84 | | - return; |
| 74 | + var mapperToUse = config.FindMapper(types); |
| 75 | + if(mapperToUse == null) |
| 76 | + { |
| 77 | + configExceptions.Add(new AutoMapperConfigurationException(memberMap.TypeMap.Types) { MemberMap = memberMap }); |
| 78 | + return; |
| 79 | + } |
| 80 | + if(!Validate(new(types, memberMap, ObjectMapper: mapperToUse))) |
| 81 | + { |
| 82 | + return; |
| 83 | + } |
| 84 | + if(mapperToUse.GetAssociatedTypes(types) is TypePair newTypes && newTypes != types) |
| 85 | + { |
| 86 | + DryRunTypeMap(newTypes, null, memberMap); |
| 87 | + } |
85 | 88 | } |
86 | | - CheckPropertyMaps(config, typeMapsChecked, typeMap); |
87 | 89 | } |
88 | | - else |
| 90 | + void CheckPropertyMaps(TypeMap typeMap) |
89 | 91 | { |
90 | | - var mapperToUse = config.FindMapper(types); |
91 | | - if (mapperToUse == null) |
92 | | - { |
93 | | - throw new AutoMapperConfigurationException(memberMap.TypeMap.Types) { MemberMap = memberMap }; |
94 | | - } |
95 | | - Validate(new(types, memberMap, ObjectMapper: mapperToUse)); |
96 | | - if (mapperToUse.GetAssociatedTypes(types) is TypePair newTypes && newTypes != types) |
| 92 | + foreach(var memberMap in typeMap.MemberMaps) |
97 | 93 | { |
98 | | - DryRunTypeMap(config, typeMapsChecked, newTypes, null, memberMap); |
| 94 | + if(memberMap.Ignored || (memberMap is PropertyMap && typeMap.ConstructorParameterMatches(memberMap.DestinationName))) |
| 95 | + { |
| 96 | + continue; |
| 97 | + } |
| 98 | + var sourceType = memberMap.SourceType; |
| 99 | + // when we don't know what the source type is, bail |
| 100 | + if(sourceType.IsGenericParameter || sourceType == typeof(object)) |
| 101 | + { |
| 102 | + continue; |
| 103 | + } |
| 104 | + DryRunTypeMap(new(sourceType, memberMap.DestinationType), null, memberMap); |
99 | 105 | } |
100 | 106 | } |
101 | | - } |
102 | | - private void CheckPropertyMaps(IGlobalConfiguration config, HashSet<TypeMap> typeMapsChecked, TypeMap typeMap) |
103 | | - { |
104 | | - foreach (var memberMap in typeMap.MemberMaps) |
| 107 | + bool Validate(ValidationContext context) |
105 | 108 | { |
106 | | - if(memberMap.Ignored || (memberMap is PropertyMap && typeMap.ConstructorParameterMatches(memberMap.DestinationName))) |
107 | | - { |
108 | | - continue; |
109 | | - } |
110 | | - var sourceType = memberMap.SourceType; |
111 | | - // when we don't know what the source type is, bail |
112 | | - if (sourceType.IsGenericParameter || sourceType == typeof(object)) |
| 109 | + foreach(var validator in Expression.Validators) |
113 | 110 | { |
114 | | - continue; |
| 111 | + try |
| 112 | + { |
| 113 | + validator(context); |
| 114 | + } |
| 115 | + catch(Exception e) |
| 116 | + { |
| 117 | + configExceptions.Add(e); |
| 118 | + return false; |
| 119 | + } |
115 | 120 | } |
116 | | - DryRunTypeMap(config, typeMapsChecked, new(sourceType, memberMap.DestinationType), null, memberMap); |
| 121 | + return true; |
117 | 122 | } |
118 | 123 | } |
119 | 124 | } |
|
0 commit comments