|
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) |
| 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) |
37 | 32 | ).ToArray(); |
38 | | - if (badTypeMaps.Length > 0) |
| 33 | + if(badTypeMaps.Length > 0) |
39 | 34 | { |
40 | | - throw new AutoMapperConfigurationException(badTypeMaps); |
| 35 | + configExceptions.Add(new AutoMapperConfigurationException(badTypeMaps)); |
41 | 36 | } |
42 | 37 | HashSet<TypeMap> typeMapsChecked = []; |
43 | | - List<Exception> configExceptions = []; |
44 | | - foreach (var typeMap in typeMaps) |
| 38 | + foreach(var typeMap in typeMaps) |
45 | 39 | { |
46 | | - try |
47 | | - { |
48 | | - DryRunTypeMap(config, typeMapsChecked, typeMap.Types, typeMap, null); |
49 | | - } |
50 | | - catch (Exception e) |
51 | | - { |
52 | | - configExceptions.Add(e); |
53 | | - } |
| 40 | + DryRunTypeMap(typeMap.Types, typeMap, null); |
54 | 41 | } |
55 | | - if (configExceptions.Count > 1) |
| 42 | + if(configExceptions.Count > 1) |
56 | 43 | { |
57 | 44 | throw new AggregateException(configExceptions); |
58 | 45 | } |
59 | | - if (configExceptions.Count > 0) |
| 46 | + if(configExceptions.Count > 0) |
60 | 47 | { |
61 | 48 | throw configExceptions[0]; |
62 | 49 | } |
63 | | - } |
64 | | - private void DryRunTypeMap(IGlobalConfiguration config, HashSet<TypeMap> typeMapsChecked, TypePair types, TypeMap typeMap, MemberMap memberMap) |
65 | | - { |
66 | | - if(typeMap == null) |
| 50 | + void DryRunTypeMap(TypePair types, TypeMap typeMap, MemberMap memberMap) |
67 | 51 | { |
68 | | - if (types.ContainsGenericParameters) |
| 52 | + if(typeMap == null) |
69 | 53 | { |
70 | | - return; |
| 54 | + if(types.ContainsGenericParameters) |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + typeMap = config.ResolveTypeMap(types.SourceType, types.DestinationType); |
71 | 59 | } |
72 | | - typeMap = config.ResolveTypeMap(types.SourceType, types.DestinationType); |
73 | | - } |
74 | | - if (typeMap != null) |
75 | | - { |
76 | | - if (typeMapsChecked.Contains(typeMap)) |
| 60 | + if(typeMap != null) |
77 | 61 | { |
78 | | - return; |
| 62 | + if(typeMapsChecked.Contains(typeMap)) |
| 63 | + { |
| 64 | + return; |
| 65 | + } |
| 66 | + typeMapsChecked.Add(typeMap); |
| 67 | + if(!Validate(new(types, memberMap, typeMap)) || !typeMap.ShouldCheckForValid) |
| 68 | + { |
| 69 | + return; |
| 70 | + } |
| 71 | + CheckPropertyMaps(typeMap); |
79 | 72 | } |
80 | | - typeMapsChecked.Add(typeMap); |
81 | | - Validate(new(types, memberMap, typeMap)); |
82 | | - if(!typeMap.ShouldCheckForValid) |
| 73 | + else |
83 | 74 | { |
84 | | - return; |
| 75 | + var mapperToUse = config.FindMapper(types); |
| 76 | + if(mapperToUse == null) |
| 77 | + { |
| 78 | + configExceptions.Add(new AutoMapperConfigurationException(memberMap.TypeMap.Types) { MemberMap = memberMap }); |
| 79 | + return; |
| 80 | + } |
| 81 | + if(!Validate(new(types, memberMap, ObjectMapper: mapperToUse))) |
| 82 | + { |
| 83 | + return; |
| 84 | + } |
| 85 | + if(mapperToUse.GetAssociatedTypes(types) is TypePair newTypes && newTypes != types) |
| 86 | + { |
| 87 | + DryRunTypeMap(newTypes, null, memberMap); |
| 88 | + } |
85 | 89 | } |
86 | | - CheckPropertyMaps(config, typeMapsChecked, typeMap); |
87 | 90 | } |
88 | | - else |
| 91 | + void CheckPropertyMaps(TypeMap typeMap) |
89 | 92 | { |
90 | | - var mapperToUse = config.FindMapper(types); |
91 | | - if (mapperToUse == null) |
| 93 | + foreach(var memberMap in typeMap.MemberMaps) |
92 | 94 | { |
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) |
97 | | - { |
98 | | - DryRunTypeMap(config, typeMapsChecked, newTypes, null, memberMap); |
| 95 | + if(memberMap.Ignored || (memberMap is PropertyMap && typeMap.ConstructorParameterMatches(memberMap.DestinationName))) |
| 96 | + { |
| 97 | + continue; |
| 98 | + } |
| 99 | + var sourceType = memberMap.SourceType; |
| 100 | + // when we don't know what the source type is, bail |
| 101 | + if(sourceType.IsGenericParameter || sourceType == typeof(object)) |
| 102 | + { |
| 103 | + continue; |
| 104 | + } |
| 105 | + DryRunTypeMap(new(sourceType, memberMap.DestinationType), null, memberMap); |
99 | 106 | } |
100 | 107 | } |
101 | | - } |
102 | | - private void CheckPropertyMaps(IGlobalConfiguration config, HashSet<TypeMap> typeMapsChecked, TypeMap typeMap) |
103 | | - { |
104 | | - foreach (var memberMap in typeMap.MemberMaps) |
| 108 | + bool Validate(ValidationContext context) |
105 | 109 | { |
106 | | - if(memberMap.Ignored || (memberMap is PropertyMap && typeMap.ConstructorParameterMatches(memberMap.DestinationName))) |
| 110 | + try |
107 | 111 | { |
108 | | - continue; |
| 112 | + foreach(var validator in Expression.Validators) |
| 113 | + { |
| 114 | + validator(context); |
| 115 | + } |
109 | 116 | } |
110 | | - var sourceType = memberMap.SourceType; |
111 | | - // when we don't know what the source type is, bail |
112 | | - if (sourceType.IsGenericParameter || sourceType == typeof(object)) |
| 117 | + catch(Exception e) |
113 | 118 | { |
114 | | - continue; |
| 119 | + configExceptions.Add(e); |
| 120 | + return false; |
115 | 121 | } |
116 | | - DryRunTypeMap(config, typeMapsChecked, new(sourceType, memberMap.DestinationType), null, memberMap); |
| 122 | + return true; |
117 | 123 | } |
118 | 124 | } |
119 | 125 | } |
|
0 commit comments